为什么我们在angular2中使用moduleId:module.id

dpk*_*k45 2 typescript angular2-template angular

为什么需要使用moduleId:module.id在使用时templateUrlAngular 2组件.

import { Component } from '@angular/core';

@Component({
  moduleId:module.id,
  selector: 'my-app',
  templateUrl:`app.component.html`,
})

export class AppComponent  { name = 'Angular 2 website'; }
Run Code Online (Sandbox Code Playgroud)

ans*_*ile 5

组件的相对资产,如@Component装饰器中的templateUrl和styleUrls.

moduleId用于解析样式表和模板的相对路径,如文档中所述.

没有模块ID

@Component({
  selector: 'my-component',
  templateUrl: 'app/components/my.component.html',
  styleUrls:  ['app/components/my.component.css'] 
})
Run Code Online (Sandbox Code Playgroud)

使用模块ID

@Component({
  moduleId: module.id,
  selector: 'my-component',
  templateUrl: 'my.component.html', 
  styleUrls:  ['my.component.css'] 
})
Run Code Online (Sandbox Code Playgroud)