对于我在Angular 2中遇到的每个示例,模板都位于组件装饰器中。
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello world</h1>'
})
export class AppComponent { }
这是正确的约定吗?当模板变得复杂时,我想这种语法会变得很麻烦。
我尝试使用templateUrl而不是template。但是据我了解,Angular然后将通过ajax加载模板。(略有相关,但是由于我已更新至2.0.0-rc.3 templateUrl似乎不再起作用,但也许我做错了)。
处理模板的最佳方法是什么?
您可以在rc3中使用templateUrl; 请参阅下面的模板代码,了解基本组件:
import { Component, OnInit } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-my-component',
templateUrl: 'my-component.component.html',
styleUrls: ['my-component.component.css']
})
export class MyComponentComponent implements OnInit {
constructor() {}
ngOnInit() {
}
}
通常,应使用templateUrl来保持组件定义的整洁。
官方ng2教程将帮助阐明一些基本问题,对我有帮助:https ://angular.io/docs/ts/latest/tutorial/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句