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 方式避免父子组件循环引用报错

在Spring框架中,`@Autowired`、`@Inject``@Resource`都是用于依赖注入的注解,但它们有一些区别: 1. **来源**: - `@Autowired`是Spring框架提供的注解。 - `@Inject`是Java的依赖注入标准(JSR-330)提供的注解。 - `@Resource`是Java的依赖注入标准(JSR-250)提供的注解。 2. **注入方式**: - `@Autowired``@Inject`默认按类型(byType)进行注入,如果需要按名称(byName)注入,可以使用`@Qualifier`注解。 - `@Resource`默认按名称(byName)进行注入,如果名称未指定,则按字段名进行注入。如果按名称找不到对应的bean,则会按类型进行注入。 3. **支持的属性**: - `@Autowired``@Inject`可以用于构造函数、字段、方法以及注解。 - `@Resource`只能用于字段setter方法。 4. **默认行为**: - `@Autowired`有一个`required`属性,默认为`true`,表示注入的bean必须存在。如果设置为`false`,则允许注入的bean不存在。 - `@Inject`没有`required`属性,默认情况下注入的bean必须存在。 - `@Resource`没有`required`属性,默认情况下注入的bean必须存在。 示例代码: ```java import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class MyBean { @Autowired private MyService myService; @Inject @Qualifier("myServiceImpl") private MyService myServiceInject; @Resource(name = "myServiceImpl") private MyService myServiceResource; // Getters and setters } ``` 在这个例子中,`@Autowired``@Inject`都通过`@Qualifier`注解来指定具体的bean,而`@Resource`直接通过`name`属性来指定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值