Tôi gặp sự cố trong đó EventEmitter không xuất giá trị cho thành phần gốc. Đây là cách tôi đang thiết lập.Góc 2 EventEmitter
main.component.ts (phiên bản ngắn)
...
@Component({
selector: 'my-app',
templateUrl: '/app/views/main.ejs',
directives: [ROUTER_DIRECTIVES],
providers:[AuthService],
inputs:['userLoggedIn']
})
@RouteConfig([
{path:'/logout', name: 'Logout', component: AuthComponent},
])
export class AppComponent implements OnInit, CanReuse{
constructor(private _auth_service: AuthService){
this._auth_service.authChanged.subscribe(data=>this.authChangedHandler(data));
}
public authChangedHandler($event){
alert($event);
}
}
...
auth.component.ts (phiên bản ngắn)
export class AuthComponent implements OnInit, CanReuse{
public loggedIn = false;
public user = {};
@Output()
authChanged: EventEmitter<string>;
public selectedTab = "/login";
constructor(private _router: Router, private _location:Location,private _http: Http, private _auth_service: AuthService){
this.authChanged = new EventEmitter();
this.authChanged.emit("authChanged");
}
}
main.ejs (phiên bản ngắn)
<nav (authChanged)="authChangedHandler($event)"></nav>
<router-outlet></router-outlet>
.210
auth.service.ts
export class AuthService {
public authChanged: EventEmitter<string>;
constructor(private _http: Http){
this.authChanged = new EventEmitter();
this.authChanged.emit("authChanged");
}
}
EDIT: Tôi đã thêm AuthService
mã mà tôi tiêm vào phần chính. Nó sẽ làm việc bây giờ nhưng không.
Bạn đã nhận được giải pháp cho nó chưa? Tôi cũng đang tìm cách nắm bắt một sự kiện được phát ra từ thành phần được thêm vào trong @RouteConfig – Sanjeev
@Sanjeev Vâng tôi đã làm. Tôi đã sử dụng Sự kiện dịch vụ. Tôi có một plunker tạo ra ở đây (https://plnkr.co/edit/WZ5HbvBP5LMSqZQtixoG?p=preview) – xmaestro
Ý tưởng là để tiêm một dịch vụ tại bootstrap và dịch vụ đó sẽ chứa eventemitter mà mỗi thành phần có thể đăng ký, trên toàn cầu. – xmaestro