首页 > 其他 > 详细

父组件和子组件联系

时间:2018-01-08 18:10:48      阅读:200      评论:0      收藏:0      [点我收藏+]

方法一:

@viewChild   引入子组件

 1、传入组件

子组件:

import {Component} from ‘@angular/core‘;

@Component({
  selector: ‘app-childen‘,
   templateUrl: ‘./user.component.html‘,
   styleUrls: [‘./user.component.css‘]
})
export class userFile{
  fun1() {
    alert(‘子组件方法‘);
   }
}


父组件:
import {Component, ViewChild} from ‘@angular/core‘;
import { userFile } from ‘./childen/user.component‘

@Component({
selector: ‘app-parent‘,
 templateUrl: ‘./parent.component.html‘,
 styleUrls: [‘./parent.component.css‘]
})

export class ParentComponent {
@ViewChild(userFile) user:
userFile ;

 OnClick() {
    this.user.fun1();
   }
}

2、传入字符串这种方式是针对指令

import {Component, ViewChild} from ‘@angular/core‘;

@Component({
selector: ‘app-parent‘,
 templateUrl: ‘./parent.component.html‘,
 styleUrls: [‘./parent.component.css‘]
})

export class ParentComponent {

@ViewChild(‘myChild‘) child;

 OnClick() {
this.child.fun1();
 }
}


使用方法
<app-component #myCilid></app-component>





方法二 @Input和@Output方法
方法三 局部变量获取子组件

<button (click)=myChild.func1()>提交</button>
<app-component #myCilid></app-component>


父组件和子组件联系

原文:https://www.cnblogs.com/mttcug/p/8243787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!