Angular 4-@ViewChild组件未定义 [英] Angular 4 - @ViewChild component is undefined
问题描述
我有一个名为ToastComponent
的敬酒通知组件,我想从任何其他组件中调用它.我是这样实现的:
I have a toast notification component called ToastComponent
which I want to call from any other component. I implemented it like this:
ToastComponent
:
export class ToastComponent implements OnInit {
constructor() {}
showToast() {
// some code
}
}
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>
在调用 On calling the 如果我从 If I take 由于在您的情况下, Since in your case, the 将 定义一个服务以基本上发出一个事件并在您的 Define a service to basically emit an event and subscribe to that event in your UtilityService: UtilityService:
您需要在 You need to inject this service in your ToastComponent: ToastComponent:
现在,您可以从所需的任何组件中调用 Now, you can call the UserManagementComponent UserManagementComponent
这篇关于Angular 4-@ViewChild组件未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!UserManagementComponent
:export class UserManagementComponent implements OnInit {
@ViewChild(ToastComponent) toast: ToastComponent;
constructor() {}
someSaveMethod() {
this.toast.showToast() // throws error below
}
}
someSaveMethod()
方法时,会出现错误,提示toast
未定义.someSaveMethod()
method, I'll get the error that toast
is undefined.app.component.html
中取出<llqa-toast></llqa-toast>
并将其放在user-management.component.html
顶部,则可以正常工作,但随后必须将其放在每个组件中.我怎样才能使它正常工作?<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. How can I get this to work?推荐答案
ToastComponent
用于大父级(AppComponent
),因此才出现此错误.避免此错误的一种方法是使用某些共享服务中定义的Subject
.我在项目中使用这种方法来显示吐司通知.这是您的操作方法:ToastComponent
is used in the grand parent (AppComponent
), that's why you are getting this error. One way to avoid this error is to use Subject
defined in some shared service. I am using that approach in my project to show toast notifications. Here is how you can do it:<llqa-toast></llqa-toast>
保留在app.component.html
中.ToastComponent
中订阅该事件.例如ToastComponent
. For example,
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);
}
}
AppModule
提供程序中注入此服务.现在subscribe
到ToastComponent
中的OnShowToast
事件. AppModule
providers. Now subscribe
to the OnShowToast
event in your 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
}
}
UtilityService
的showToast()
方法.例如,showToast()
method of the UtilityService
from any component you want. For example,
export class UserManagementComponent implements OnInit {
// You dont need this now
// @ViewChild(ToastComponent) toast: ToastComponent;
constructor(private readonly utilityService: UtilityService) {}
someSaveMethod() {
this.utilityService.showToast();
}
}