[英]Angular 2, component inside main component
我在 angular.io 上玩过 angular 2 演示。 现在我想创建一个新组件并在我的应用程序中使用它。
我当前的应用程序:
import {Component, Template, bootstrap} from 'angular2/angular2';
import {UsersComponent} from './components/users/component.js';
// Annotation section
@Component({
selector: 'my-app'
})
@Template({
url:"app.html"
})
// Component controller
class MyAppComponent {
newName:string;
names:Array<string>;
constructor() {
this.name = 'Florian';
}
showAlert(){
alert("It works");
}
addName(){
names.push(newName);
newName = "";
}
}
bootstrap(MyAppComponent);
我的组件:
import {Component, Template, bootstrap} from 'angular2/angular2';
// Annotation section
@Component({
selector: 'users'
})
@Template({
url:"./template.html"
})
// Component controller
class UsersComponent {
newName:string;
names:Array<string>;
constructor() {
}
addName(){
names.push(newName);
newName = "";
}
}
我不确定我的用户组件的语法是否完全正确。
我的应用模板:
<h1>Hello {{ name }}</h1>
<h2>{{2+2}}</h2>
<hr />
<button (click)="showAlert()">Click to alert</button>
<users></users>
那么如何连接 User 组件呢?
我在控制台中得到的错误:
"Error loading "components/users/component.js" at <unknown>↵Error loading "components/users/component.js" from "app" at http://localhost:8080/app.es6↵Cannot read property 'replace' of undefined"
Angular 日历演示有两个嵌套组件(日历和日历单元) https://github.com/djsmith42/angular2_calendar.git 。 从我所看到的,您的 MyAppComponent 应该从@Template 的指令列表中引用用户组件,如下所示:
@Template({
url: System.baseURL+'app/components/calendar/calendar.html',
directives: [
Foreach,
If,
CalendarCell
]
})
在Angular2
web https://angular.io/docs/ts/latest/tutorial/toh-pt3.html# 的这个链接中,您可以看到在@Components
中添加directives: [...]
例如:
@Component({
selector: 'my-app',
directives: [UsersComponent]
})
但否则链接在组件内部使用模板,如果您在组件外部使用@Template({ url:"app.html"})
,不知道这是否有效。
您可以查看文件名app.appcomponet.ts
以了解更多详细信息,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.