[英]Angular 4 - @ViewChild component is undefined
I have a toast notification component called ToastComponent
which I want to call from any other component. 我有一个名为
ToastComponent
的Toast通知组件,我想从任何其他组件调用它。 I implemented it like this: 我这样实现了:
ToastComponent
: ToastComponent
:
export class ToastComponent implements OnInit {
constructor() {}
showToast() {
// some code
}
}
app.component.html
: app.component.html
:
<llqa-main-container>
<llqa-header></llqa-header>
<div class="content-container">
<main class="content-area">
<llqa-toast></llqa-toast> <!-- ToastComponent which I want to call -->
<router-outlet></router-outlet>
</main>
</div>
</llqa-main-container>
UserManagementComponent
which is inside the <router-outlet>
: UserManagementComponent
位于<router-outlet>
:
export class UserManagementComponent implements OnInit {
@ViewChild(ToastComponent) toast: ToastComponent;
constructor() {}
someSaveMethod() {
this.toast.showToast() // throws error below
}
}
On calling the someSaveMethod()
method, I'll get the error that toast
is undefined. 在调用
someSaveMethod()
方法时,我将得到toast
未定义的错误。
If I take <llqa-toast></llqa-toast>
out of the app.component.html
and put it on top of the user-management.component.html
, it works fine, but then I have to put it in every component. 如果我参加
<llqa-toast></llqa-toast>
出的app.component.html
并把它放在顶部user-management.component.html
,它工作正常,但后来我不得不把它放在每零件。 How can I get this to work? 我怎样才能让它发挥作用?
Since in your case, the ToastComponent
is used in the grand parent ( AppComponent
), that's why you are getting this error. 因为在您的情况下,
ToastComponent
用于ToastComponent
( AppComponent
),这就是您收到此错误的原因。 One way to avoid this error is to use Subject
defined in some shared service. 避免此错误的一种方法是使用某些共享服务中定义的
Subject
。 I am using that approach in my project to show toast notifications. 我在我的项目中使用该方法来显示吐司通知。 Here is how you can do it:
以下是如何做到这一点:
Keep your <llqa-toast></llqa-toast>
in app.component.html
. 保持你的
<llqa-toast></llqa-toast>
在app.component.html
。
Define a service to basically emit an event and subscribe to that event in your ToastComponent
. 定义服务以基本上发出事件并在
ToastComponent
订阅该事件。 For example, 例如,
UtilityService:
UtilityService:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class UtilityService {
public OnShowToast = new Subject<boolean>();
public showToast(): void {
this.OnShowToast.next(true);
}
}
You need to inject this service in your AppModule
providers. 您需要在
AppModule
提供程序中注入此服务。 Now subscribe
to the OnShowToast
event in your ToastComponent
. 现在
subscribe
到OnShowToast
在事件ToastComponent
。
ToastComponent:
ToastComponent:
import { UtilityService } from './path/to/the/utility.service';
export class ToastComponent implements OnInit {
constructor(private readonly utilityService: UtilityService) { }
ngOnInit() {
this.utilityService.OnShowToast.subscribe(value =>
{
this.showToast();
});
}
private showToast() {
// some code
}
}
Now, you can call the showToast()
method of the UtilityService
from any component you want. 现在,你可以调用
showToast()
的方法UtilityService
从任何你想要的组件。 For example, 例如,
UserManagementComponent
UserManagementComponent
export class UserManagementComponent implements OnInit {
// You dont need this now
// @ViewChild(ToastComponent) toast: ToastComponent;
constructor(private readonly utilityService: UtilityService) {}
someSaveMethod() {
this.utilityService.showToast();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.