如何在Angular 6中从父组件调用子组件的方法
如何在Angular 6中从父组件调用子组件的方法
父组件
导入{ Component } from '@angular/core';
导入{ ChildComponent } from './notify.component';
@Component({
选择器: '我的应用',
模板:
`
<按钮(点击)="submit()">调用子组件方法按钮>
`
})
export class AppComponent {
构造函数(private childComp: ChildComponent) {
}
提交(): void {
// 执行子组件方法
// 此行在编译时正确,但在运行时会给我一个与静态注入器相关的错误
childComp.callMethod();
}
}
子组件
导入{ Component, OnInit } from '@angular/core';
@Component({
选择器: '子',
模板: '
子组件{{test}}
'
})
export class ChildComponent implements OnInit {
测试:string;
构造函数() { }
ngOnInit() { }
callMethod(): void {
console.log('成功执行。');
this.test = '我';
}
}
我在静态注入器上遇到了错误,我无法在父组件中注入子组件。以下是错误。
StaticInjectorError(AppModule)[AppComponent -> ChildComponent]:
StaticInjectorError[ChildComponet]:
NullInjectorError: No provider for ChildComponet!
我已经在Appmodule中添加了引用,并在声明中添加了组件。仍然遇到相同的问题。
请帮帮我!
在Angular 6中,如果我们想从父组件调用子组件的方法,我们需要遵循一些步骤。下面是产生这个问题的原因以及解决方法的整理。
首先,我们需要在app.module.ts文件中的declarations部分添加ChildComponent。这样,我们才能在应用程序中使用子组件。但是,有时在添加子组件后,我们可能会遇到以下错误信息:"error TS1146: Declaration expected.",并且在子组件下方会有红色波浪线。
为了解决这个错误,我们需要检查以下几点:
1. 确保在app.module.ts文件中正确引入了子组件。我们可以通过使用import语句来引入子组件的类。例如:
import { ChildComponent } from './child.component';
2. 确保在app.module.ts文件中的declarations部分中正确声明了子组件。我们需要将子组件添加到declarations数组中,以便Angular能够识别和使用它。例如:
declarations: [ChildComponent]
3. 确保在子组件的文件中正确导出子组件的类。我们需要使用export关键字将子组件的类导出,以便在父组件中使用它。例如:
export class ChildComponent { ... }
通过遵循上述步骤,我们应该能够成功地将子组件添加到应用程序中,并能够从父组件调用子组件的方法。这样,我们就可以实现父子组件之间的通信和交互。
在Angular 6中,要从父组件调用子组件的方法,我们需要将子组件正确地添加到app.module.ts文件的declarations部分中。如果我们遇到错误TS1146或红色波浪线的问题,我们需要检查是否正确导入和声明了子组件,并正确导出了子组件的类。通过遵循这些步骤,我们可以成功地解决这个问题并实现父子组件之间的通信。
如何从父组件中调用子组件的方法,在Angular 6中,可以通过引入一种称为ViewChild的概念来实现,它允许将一个组件注入到另一个组件中,从而使父组件可以访问其属性和方法。
具体实现如下:
首先,在父组件中引入ViewChild和AfterViewInit,然后使用@ViewChild装饰器将子组件注入到父组件中。
import { Component, ViewChild, AfterViewInit } from 'core'; import { ChildComponent } from "../child/child.component"; @Component({ selector: 'app-parent', template: ` Message: {{ message }}`, styleUrls: ['./parent.component.css'] }) export class ParentComponent implements AfterViewInit { @ViewChild(ChildComponent) child: ChildComponent; message: string; constructor() { } ngAfterViewInit() { this.message = this.child.message; } }
然后,在子组件中定义message属性和构造函数。
import { Component} from 'core'; @Component({ selector: 'app-child', template: ` `, styleUrls: ['./child.component.css'] }) export class ChildComponent { message = 'Hola Mundo!'; constructor() { } }
注意,父组件中的`@ViewChild(ChildComponent) child: ChildComponent;`表示将子组件注入到父组件的child属性中。然后在父组件的ngAfterViewInit方法中,可以通过this.child来访问子组件的属性和方法。
此外,如果在引入ViewChild时出现错误"error TS1146: Declaration expected.",可以尝试添加以下语句`import {Component, View} from 'angular2/core';`来解决问题。
如何在Angular 6中从父组件调用子组件的方法
问题的出现原因:我想调用子组件的方法,我的要求是,如果变量的值改变或保持不变,应调用子组件的特定函数,该函数将从服务中返回数据。
解决方法:您可以通过在父组件中使用
以下为示例代码:
Parent.html:
Parent.ts:
import { Component, ViewChild } from '/core'; import { ChildComponent } from './child.component'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor() { } @ViewChild(ChildComponent) private myChild: ChildComponent; submit() { this.myChild.callMethod(); } }
Child.ts:
import { Component } from '/core'; @Component({ selector: 'child', template: `Child component {{test}}
` }) export class ChildComponent { test = 0; callMethod() { console.log('successfully executed.'); this.test++; } }
在Submit函数中,只需更改输入即可调用您的方法。
如果值保持不变,OnChange生命周期钩子将不会被调用。是否有可用于相同目的的生命周期钩子(值更改或保持不变时调用函数)?我正在从父组件的函数中调用该函数。
我已经有两个可用的组件,并且希望在这两个组件之间进行通信。不能为此创建共享服务。因为我必须从子组件的方法中进行服务调用。所有都是预定义的,只是想在子组件中调用函数。
(ChildComponent)private myChild:ChildComponent;我得到了这个错误“错误TS1146:需要声明。”在私有下面的myChild下面出现红色波浪线。
尝试只是:(ChildComponent) myChild;
已经尝试过,红色波浪线移到了myChild,消息仍然相同:(
将您的新代码发送给我!这似乎是一个分号问题
这个有效了!我只需要将该行移动到一般声明区域,我将该行放在了私有函数中。我真是个笨蛋,谢谢你的帮助!!
太棒了!这就是我一直在寻找的。谢谢
大家可以按照此链接 [gist.github.com/pradipkumardesai/… 获取一个简单且正确的实现,这对我很有用。