一、@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的更多相关文章

  1. ng2父子模块数据交互

    一.父模块向子模块传值 //父html <my-child [childdata]="parentdata"></my-child> 其中,my-child ...

  2. Vue 非父子组件通信方案

    Vue 非父子组件通信方案 概述 在 Vue 中模块间的通信很普遍 如果是单纯的父子组件间传递信息,父组件可以使用 props 将数据向下传递到子组件,而在子组件中可以使用 events (父组件需要 ...

  3. 三大前端框架(react、vue、angular2+)父子组件通信总结

    公司业务需要,react.vue.angular都有接触[\无奈脸].虽然说可以拓展知识广度,但是在深度上很让人头疼.最近没事的时候回忆各框架父子组件通信,发现很模糊,于是乎稍微做了一下功课,记录于此 ...

  4. 从$emit 到 父子组件通信 再到 eventBus

    故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...

  5. vue组件通信之父子组件通信

    准备工作: 首先,新建一个项目,如果这里有不会的同学,可以参考我转载过的文章 http://www.cnblogs.com/Sky-Ice/p/8875958.html  vue 脚手架安装及新建项目 ...

  6. 总结Vue第二天:自定义子组件、父子组件通信、插槽

    总结Vue第二天:自定义子组件.父子组件通信.插槽 一.组件: 组件目录 1.注册组件(全局组件.局部组件和小demo) 2.组件数据存放 3.父子组件通信(父级向子级传递数据.子级向父级传递数据) ...

  7. 关于React的父子组件通信等等

    //==================================================此处为父子组件通信 1.子组件调用父组件: 父组件将子组件需要调用方法存入props属性内,子组 ...

  8. Vue 非父子组件通信

    组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求.组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信.前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有 ...

  9. vue父子组件通信

    一.父子组件间通信 vue.js 2.0提供了一个ref 的属性: 可以为子组件指定一个索引id 父组件: <template> <div id='user-login'> & ...

随机推荐

  1. matlab biplot 符号的困惑

    在matlab中做Principal component Analysis 时,常要用biplot 函数来画图,表示原分量与主分量(principal component)之间的关系,以及原始观察数据 ...

  2. 目标跟踪之camshift---opencv中meanshift和camshift例子的应用

    在这一节中,主要讲目标跟踪的一个重要的算法Camshift,因为它是连续自使用的meanShift,所以这2个函数opencv中都有,且都很重要.为了让大家先达到一个感性认识.这节主要是看懂和运行op ...

  3. Unity3D GUI中的图片尾随鼠标旋转脚本

    var Mid : Texture2D; var mouse : Texture2D; //鼠标图片 var mousePs = Vector2.zero; //鼠标的位置 private var a ...

  4. ES通过API调整设置

    1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0

  5. AWS:4.VPC

    主要介绍 1.Amazon混合云 2.将EC2加入VPC 3.VPC经典场景 4.VPC安全保障 Amazon混合云 : 在公有云的基础上创建私有云 VPC概念 VPC(VPC Virtual Pri ...

  6. python venv虚拟环境

    1 目的 给python应用一个独立的运行环境,独立于其它的python应用也独立于系统的python环境. 环境升级不影响其它应用. 避免包冲突. 2 创建方式 2.1 pycharm中创建 pro ...

  7. java参数的值传递和引用传递

    今天抽了点时间继续啃java核心基础,即使出来做web挺长时间了,始终觉得基础极其重要. 遇到了java参数的传递类型,豁然开朗之时不忘写下记录. java中采用的总是值传递,包括对对象参数的传递,采 ...

  8. Why Use C++/CLI?

    来源:http://www.asawicki.info/Download/Productions/Publications/CPP_CLI_tutorial.pdf Why Use C++/CLI? ...

  9. 20145239 杜文超 实验四 Android开发基础

    20145239实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理 ...

  10. tkinter之canvas(画布)

    画布的例子: from tkinter import * root=Tk() root.title('简易绘图') can=Canvas(root,width=400,height=300,bg='# ...