我在 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"