【问题标题】:Component not rendering Angular 2 Typescript组件未呈现 Angular 2 Typescript
【发布时间】:2016-06-04 09:16:51
【问题描述】:

我正在尝试学习 Angular 2,经过大量错误消息后,我终于让路由正常工作,并且可以显示我想要的组件。现在我想展示我的主页组件,并且我希望这个组件中有一个导航栏组件。

Home.ts:

import {Component, View} from 'angular2/core';
import {Navbar} from './navbar/navbar'

@Component({
  selector: 'home'
})

@View({
  templateUrl: '/app/home/home.html'
})

export class Home {

    constructor:(public navbar: Navbar) {
    }

}

home.html:

<p>NICE!</p>
<navbar></navbar>

navbar.ts:

import {Component, View} from 'angular2/core';

@Component({
  selector: 'navbar'
})

@View({
  template: `
  <ul>
    <li>This is the navbar</li>
  </ul>
  `
})

export class Navbar{
}

主页正在显示,但当我检查它时,标签只是停留在&lt;navbar&gt;&lt;/navbar&gt;。我做错了什么?

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    如果你想要navbar组件,你需要在父组件的directives属性中添加它,而不是在它的构造函数中,像这样:

    @Component({
      selector: 'home',
      templateUrl: '/app/home/home.html',
      directives: [ Navbar ]
    })
    export class Home {
      constructor() {
    
      }
    }
    

    【讨论】:

    • 谢谢!这确实是这个问题的答案,但恐怕这会让我陷入下一个错误:(。现在我的路由器又出问题了。
    • 我的导航栏似乎没有编译,再次编译它现在就像一个魅力!谢谢!
    猜你喜欢
    • 2018-04-14
    • 2020-04-25
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2017-07-21
    相关资源
    最近更新 更多