angular2里的directive理解?

export class AppComponent {}

我也export了HeroComponent模块,然后在app.component一开始就import进来,然后在directive加入这些模块。然后template才能识别相应的selector???

不是很明白这句话,我引入的是HeroComponent里的类吗。然后directive之后,我能怎么用?是继承他的类吗

阅读 3.6k
3 个回答

import HeroComponent...
@Component({

  ........

directive:[HeroComponent]
})
class AppComponent{}

import只有一个作用,就是把类,变量或者其他东西引入进来,import本身只保证当类需要的时候,存在,可用。

directives的作用就是告诉模版渲染引擎某个标签有特殊意义,当前template渲染的时候,如果识别到directive内指定的标签就自动调用相应的组件,填充这个标签。

import {HeroComponent} from ...
@Component({
  template: `
  <element>...</element>
  <element>...</element>
  ...
  <hero-component>...</hero-component>
  `
  directives: [HeroComponent]
})

@Component({
  selector: "hero-compoennt"
})
export class HeroComponent {
  
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题