ng2父子模块通信@ViewChild和@Inject
一、@ViewChild
父组件中使用@ViewChild拿到子组件的变量和方法(父组件可调用子组件的方法和变量)
parent.component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';
@Component({
selector: 'my-parent',
templateUrl: './parent.component.html',
styleUrls: [ './parent.component.css' ],
})
export class ParentComponent implements OnInit {
//通过@ViewChild注册子组件
@ViewChild(ChildComponent) public child:ChildComponent;
public countNum: number;
public firstName:string = "Jeck";
public fullName:string = "";
constructor() {}
ngOnInit(): void {
}
displayFull(){
this.fullName = this.firstName + this.child.lastName;
console.log(this.fullName) //"Jeck wang"
}
}
child.component.ts:
import { Component, OnInit} from '@angular/core';
@Component({
selector: 'my-child',
templateUrl: './child.component.html',
styleUrls: [ './child.component.css' ],
})
export class ChildComponent implements OnInit {
public lastName:string = "wang";
constructor() {}
ngOnInit(): void {
}
}
二、@Inject
子组件中使用@Inject调用父组件中的变量和方法
parent.component.ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-parent',
templateUrl: './parent.component.html',
styleUrls: [ './parent.component.css' ],
})
export class ParentComponent implements OnInit {
constructor() {}
ngOnInit(): void {
}
sayHello(){
console.log("Hello!")
}
}
child.component.ts:
import { Component, OnInit, Inject, forwardRef} from '@angular/core';
import { ParentComponent } from './parent.component';
@Component({
selector: 'my-child',
templateUrl: './child.component.html',
styleUrls: [ './child.component.css' ],
})
export class ChildComponent implements OnInit {
constructor(
@Inject(forwardRef(()=>ParentComponent)) public parent:ParentComponent
) {}
ngOnInit(): void {
this.parent.sayHello(); //"Hello!"
}
}
注意:如果父子模块通过以上方式相互引用,请在父模块中使用 @ViewChild(forwardRef(()=>ChildComponent)) public child:ChildComponent 方式避免父子组件循环引用报错
ng2父子模块通信@ViewChild和@Inject的更多相关文章
- ng2父子模块数据交互
一.父模块向子模块传值 //父html <my-child [childdata]="parentdata"></my-child> 其中,my-child ...
- Vue 非父子组件通信方案
Vue 非父子组件通信方案 概述 在 Vue 中模块间的通信很普遍 如果是单纯的父子组件间传递信息,父组件可以使用 props 将数据向下传递到子组件,而在子组件中可以使用 events (父组件需要 ...
- 三大前端框架(react、vue、angular2+)父子组件通信总结
公司业务需要,react.vue.angular都有接触[\无奈脸].虽然说可以拓展知识广度,但是在深度上很让人头疼.最近没事的时候回忆各框架父子组件通信,发现很模糊,于是乎稍微做了一下功课,记录于此 ...
- 从$emit 到 父子组件通信 再到 eventBus
故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...
- vue组件通信之父子组件通信
准备工作: 首先,新建一个项目,如果这里有不会的同学,可以参考我转载过的文章 http://www.cnblogs.com/Sky-Ice/p/8875958.html vue 脚手架安装及新建项目 ...
- 总结Vue第二天:自定义子组件、父子组件通信、插槽
总结Vue第二天:自定义子组件.父子组件通信.插槽 一.组件: 组件目录 1.注册组件(全局组件.局部组件和小demo) 2.组件数据存放 3.父子组件通信(父级向子级传递数据.子级向父级传递数据) ...
- 关于React的父子组件通信等等
//==================================================此处为父子组件通信 1.子组件调用父组件: 父组件将子组件需要调用方法存入props属性内,子组 ...
- Vue 非父子组件通信
组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求.组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信.前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有 ...
- vue父子组件通信
一.父子组件间通信 vue.js 2.0提供了一个ref 的属性: 可以为子组件指定一个索引id 父组件: <template> <div id='user-login'> & ...
随机推荐
- uget + aria2
@.Ubuntu 16.04下安装 原文:http://blog.csdn.net/xiaohouye/article/details/54603198 安装完成后可在终端输入:uget-gtk打开u ...
- 各种jar包下方法的使用
commons-codec-1.6.jar: DigestUtils.md5Hex(String str); httpclient-4.2.2.jar: HttpClient client=new D ...
- Notepad工具使用小技巧
工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...
- React-Native开源项目学习
https://github.com/liuhongjun719/react-native-DaidaiHelperNew 借贷助手https://github.com/liuhongjun719/r ...
- Hadoop实战-Flume之Source multiplexing(十五)
a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...
- Struts2常见面试点
01. 三层和MVC的区别 http://blog.csdn.net/csh624366188/article/details/7183872 http://www.cnblogs.com/zdxs ...
- 创建laravel项目
下载项目到本地 git clone https://github.com/251068550/LaraBlog.git compoer安装 cd LaraBlog composer install 如 ...
- 我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换
今天看到两个面试题,居然都做错了.通过这两个面试题,也加深对三目运算是的自动类型转换的理解. 题目1.以下代码输出结果是(). public class Test { public static vo ...
- Android之ProgressBar读取文件进度解析
ProgressBar进度条, 分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案例大多都是通过Button点 击增加.减少进度值,使 ...
- LeetCode 017 4Sum
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...