Tôi đã chơi với bản trình diễn góc 2 trên angular.io. Bây giờ tôi muốn tạo một thành phần mới và sử dụng nó trong ứng dụng của tôi.Góc 2, thành phần bên trong thành phần chính
ứng dụng hiện tại của tôi:
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);
thành phần của tôi:
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 = "";
}
}
Tôi không chắc chắn nếu cú pháp của thành phần người dùng của tôi là đúng cả.
My ứng dụng mẫu:
<h1>Hello {{ name }}</h1>
<h2>{{2+2}}</h2>
<hr />
<button (click)="showAlert()">Click to alert</button>
<users></users>
Vì vậy, làm thế nào để cấp điện cho thành phần người dùng?
Các lỗi tôi nhận được trong giao diện điều khiển:
"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"
Bạn đã đúng, cảm ơn ví dụ. Ứng dụng của tôi thực sự không có mảng chỉ thị. Url mẫu của tôi về thành phần Người dùng của tôi sai (phải là đường dẫn tuyệt đối). Và tôi đã không thêm thư mục thành phần của tôi vào System.paths của tôi trong tệp html. – user1613512
Nit nhỏ: Tôi nghĩ bạn cần một 'const' trước danh sách chỉ thị. Đây là một vi viết lên với một hướng dẫn nhanh chóng sửa đổi: http://bq9.blogspot.ch/2015/03/angular2-dart-nested-components.html – buraq
@buraq nope, đó là cho phi tiêu. Người này đang sử dụng js. –