admin管理员组文章数量:1023773
I'm trying to make two angular ponents and I want to call a function from the first ponent in the second ponent. When I try this I get following error message: Cannot red property 'functionName' of undefined
. How can this be solved?
Here a link of an example:
I'm trying to make two angular ponents and I want to call a function from the first ponent in the second ponent. When I try this I get following error message: Cannot red property 'functionName' of undefined
. How can this be solved?
Here a link of an example: https://stackblitz./edit/angular-rre4gb
Share Improve this question asked Aug 14, 2017 at 10:37 LeonzenLeonzen 1,2755 gold badges14 silver badges31 bronze badges 1- for a quick start, check my updated answer. – Hamid Asghari Commented Aug 14, 2017 at 10:51
4 Answers
Reset to default 3That's because the ponent you want to call its function, is not instantiated.
for ponent munication you can use a service
instead:
Service
@Injectable()
export class MyService {
myCustomFunction(){
}
}
Component
in your ponent:
@Component({
selector: 'my-ponent',
providers: [ MyService ]
})
export class MyComponent {
// inject your service to make it available
constructor(private service: MyService){}
doStuff(){
// call function which is located in your service
this.service.myCustomFunction();
}
}
As others have stated, I would prefer a shared service with a Subject
among these ponents.
service:
@Injectable()
export class SharedService {
mySubject = new Subject();
}
WorldComponent (subscriber):
export class WorldComponent {
constructor(private sharedService: SharedService){
this.sharedService.mySubject.subscribe((data)=>{
this.worldFunction();
})
}
HelloComponent(publisher):
public helloFunction() {
alert('Hello');
this.sharedService.mySubject.next(true);
}
You can find the updated example here: https://stackblitz./edit/angular-rnvmkq?file=app%2Fworld.ponent.ts
The best way to share information between multiple ponents is generally through a service.
Create a separate file: file.service.ts
Provide the service in the app.module.ts file
Inject the service into each ponent. Then you'll have access to the variables in both ponents
See this: https://angular.io/tutorial/toh-pt4
the reason of the error is that the hello ponent is not imported, but instead of calling a ponent from another, you should use a service in between, as other answers already suggested.
I'm trying to make two angular ponents and I want to call a function from the first ponent in the second ponent. When I try this I get following error message: Cannot red property 'functionName' of undefined
. How can this be solved?
Here a link of an example:
I'm trying to make two angular ponents and I want to call a function from the first ponent in the second ponent. When I try this I get following error message: Cannot red property 'functionName' of undefined
. How can this be solved?
Here a link of an example: https://stackblitz./edit/angular-rre4gb
Share Improve this question asked Aug 14, 2017 at 10:37 LeonzenLeonzen 1,2755 gold badges14 silver badges31 bronze badges 1- for a quick start, check my updated answer. – Hamid Asghari Commented Aug 14, 2017 at 10:51
4 Answers
Reset to default 3That's because the ponent you want to call its function, is not instantiated.
for ponent munication you can use a service
instead:
Service
@Injectable()
export class MyService {
myCustomFunction(){
}
}
Component
in your ponent:
@Component({
selector: 'my-ponent',
providers: [ MyService ]
})
export class MyComponent {
// inject your service to make it available
constructor(private service: MyService){}
doStuff(){
// call function which is located in your service
this.service.myCustomFunction();
}
}
As others have stated, I would prefer a shared service with a Subject
among these ponents.
service:
@Injectable()
export class SharedService {
mySubject = new Subject();
}
WorldComponent (subscriber):
export class WorldComponent {
constructor(private sharedService: SharedService){
this.sharedService.mySubject.subscribe((data)=>{
this.worldFunction();
})
}
HelloComponent(publisher):
public helloFunction() {
alert('Hello');
this.sharedService.mySubject.next(true);
}
You can find the updated example here: https://stackblitz./edit/angular-rnvmkq?file=app%2Fworld.ponent.ts
The best way to share information between multiple ponents is generally through a service.
Create a separate file: file.service.ts
Provide the service in the app.module.ts file
Inject the service into each ponent. Then you'll have access to the variables in both ponents
See this: https://angular.io/tutorial/toh-pt4
the reason of the error is that the hello ponent is not imported, but instead of calling a ponent from another, you should use a service in between, as other answers already suggested.
本文标签: javascriptAngular call function from other componentStack Overflow
版权声明:本文标题:javascript - Angular: call function from other component - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745578920a2157190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论