Angular组件间怎么进行交互?常用交互方法介绍

组件之间的交互主要是在主从组件之间进行交互。那么angular组件之间怎么进行交互?下面本篇文章给大家介绍一下angular组件间进行常用交互的方法。

Angular组件间怎么进行交互?常用交互方法介绍

【相关教程推荐:《angular教程》】

1、通过输入型绑定把数据从父组件传到子组件

child.component.ts

export class ChildComponent implements OnInit {  @Input() hero: any;  @Input('master') masterName: string;      // 第二个 @Input 为子组件的属性名 masterName 指定一个别名 master  constructor() { }  ngOnInit(): void {  }}

登录后复制

child.component.html

  

child works!

  

{{hero?.name}} says:

  

I, {{hero?.name}}, am at your service, {{masterName}}.

登录后复制

parent.component.ts

export class ParentComponent implements OnInit {  hero = {name: 'qxj'}  master = 'Master'  constructor() {  }  ngOnInit(): void {  }}

登录后复制

parent.component.html


登录后复制

2、父组件监听子组件的事件

child.component.ts

export class ChildComponent implements OnInit {  @Input()  name: string;  @Output() voted = new EventEmitter();  didVote = false;  vote(agreed: boolean) {    this.voted.emit(agreed);    this.didVote = true;  }  constructor() { }  ngOnInit(): void {  }}

登录后复制

child.component.html

{{name}}

登录后复制

parent.component.ts

export class ParentComponent implements OnInit {  agreed = 0  disagreed = 0  voters = ['Narco', 'Celeritas', 'Bombasto']  onVoted(agreed: boolean) {    agreed ? this.agreed++ : this.disagreed++  }  constructor() {  }  ngOnInit(): void {  }}

登录后复制

parent.component.html

Should mankind colonize the Universe?

Agree: {{agreed}}, Disagree: {{disagreed}}

登录后复制

test1

3、父组件与子组件通过本地变量互动

父组件不能使用数据绑定来读取子组件的属性或调用子组件的方法。但可以在父组件模板里,新建一个本地变量来代表子组件,然后利用这个变量来读取子组件的属性和调用子组件的方法,如下例所示。

子组件 CountdownTimerComponent 进行倒计时,归零时发射一个导弹。start 和 stop 方法负责控制时钟并在模板里显示倒计时的状态信息。

child.component.ts

export class ChildComponent implements OnInit, OnDestroy {  intervalId = 0  message = ''  seconds = 11  clearTimer() {    clearInterval(this.intervalId)  }  ngOnInit() {    this.start()  }  ngOnDestroy() {    this.clearTimer()  }  start() {    this.countDown()  }  stop() {    this.clearTimer()    this.message = `Holding at T-${this.seconds} seconds`  }  private countDown() {    this.clearTimer()    this.intervalId = window.setInterval(() => {      this.seconds -= 1      if (this.seconds === 0) {        this.message = 'Blast off!'      } else {        if (this.seconds 

child.component.html

{{message}}

登录后复制

parent.component.ts

export class ParentComponent implements OnInit {  constructor() {  }  ngOnInit(): void {  }}

登录后复制

parent.component.html

Countdown to Liftoff (via local variable)

{{child.seconds}}

登录后复制

countdown timer

4、父组件调用@ViewChild()

这个本地变量方法是个简单便利的方法。但是它也有局限性,因为父组件-子组件的连接必须全部在父组件的模板中进行。父组件本身的代码对子组件没有访问权。

如果父组件的需要读取子组件的属性值或调用子组件的方法,就不能使用本地变量方法。

当父组件需要这种访问时,可以把子组件作为 ViewChild,***注入***到父组件里面。

countdown-parent.component.ts

import {AfterViewInit, Component, ViewChild} from '@angular/core'import {ChildComponent} from '../child/child.component'@Component({  selector: 'app-parent-vc',  template: `    

Countdown to Liftoff (via ViewChild)

            {{ seconds() }}      `,})export class CountdownParentComponent implements AfterViewInit {  @ViewChild(ChildComponent)  private timerComponent: ChildComponent  seconds() {    return 0  }  ngAfterViewInit() {    // Redefine `seconds()` to get from the `ChildComponent.seconds` ...    // but wait a tick first to avoid one-time devMode    // unidirectional-data-flow-violation error    setTimeout(() => {      this.seconds = () => this.timerComponent.seconds    }, 0)  }  start() {    this.timerComponent.start()  }  stop() {    this.timerComponent.stop()  }}

登录后复制

更多编程相关知识,请访问:编程入门!!

以上就是Angular组件间怎么进行交互?常用交互方法介绍的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2711183.html

(0)
上一篇 2025年3月7日 21:00:56
下一篇 2025年3月2日 12:56:48

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论