Angular 8: 输入/输出 vs 服务
创始人
2024-10-17 18:30:12
0

在Angular中,输入/输出和服务是两种常见的组件和模块之间通信的方式。下面是一个示例,展示了如何在Angular 8中使用输入/输出和服务。

首先,我们创建一个名为parent.component.ts的父组件,其中包含一个输入属性和一个输出属性。

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

@Component({
  selector: 'app-parent',
  template: `
    

Parent Component

`, }) export class ParentComponent { data: string = 'Hello World'; handleOutput(data: string) { console.log(data); } }

在父组件中,我们使用inputData将数据传递给子组件,并使用outputData监听子组件的输出。

接下来,我们创建一个名为child.component.ts的子组件,它接收输入数据并触发输出事件。

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    

Child Component

{{ inputData }}

`, }) export class ChildComponent { @Input() inputData: string; @Output() outputData = new EventEmitter(); handleClick() { this.outputData.emit('Output from child component'); } }

在子组件中,我们使用@Input()装饰器接收父组件传递的数据,并使用@Output()装饰器触发输出事件。

最后,我们需要将这两个组件添加到模块中,并将父组件添加到应用的根组件中。在app.module.ts中,我们将父组件和子组件添加到declarations数组中。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { ParentComponent } from './parent.component';
import { ChildComponent } from './child.component';

@NgModule({
  imports: [BrowserModule],
  declarations: [AppComponent, ParentComponent, ChildComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

这样,我们就完成了使用输入/输出进行组件间通信的示例。

另一种常见的通信方式是使用服务。在Angular中,服务是一个可注入的类,用于共享数据和功能。下面是一个示例,展示了如何在Angular 8中使用服务进行组件间通信。

首先,我们创建一个名为data.service.ts的服务,其中包含一个共享数据的属性和一个共享方法。

import { Injectable } from '@angular/core';

@Injectable()
export class DataService {
  sharedData: string = 'Shared data from service';

  getSharedData() {
    return this.sharedData;
  }
}

在服务中,我们定义了一个sharedData属性和一个getSharedData方法,用于获取共享数据。

接下来,我们在父组件和子组件中注入这个服务,并使用它共享数据。

import { Component } from '@angular/core';
import { DataService } from './data.service';

@Component({
  selector: 'app-parent',
  template: `
    

Parent Component

{{ sharedData }}

`, }) export class ParentComponent { sharedData: string; constructor(private dataService: DataService) { this.sharedData = dataService.getSharedData(); } } @Component({ selector: 'app-child', template: `

Child Component

{{ sharedData }}

`, }) export class ChildComponent { sharedData: string; constructor(private dataService: DataService) { this.sharedData = dataService.getSharedData(); } }

在父组件和子组件中,我们通过构造函数注入了DataService,并使用getSharedData方法获取共享数据。

最后,我们需要将这些组件和服务添加到模块中,并将父组件添加到应用的根组件中。同样,在`

相关内容

热门资讯

突发!哈糖大菠萝怎么挂,新众亿... 突发!哈糖大菠萝怎么挂,新众亿辅助,其实是有辅助方法(哔哩哔哩)1、用户打开应用后不用登录就可以直接...
今年以来!wpk德州局透视,潮... 今年以来!wpk德州局透视,潮汕掌上娱乐脚本,都是是真的有辅助app(哔哩哔哩)1、很好的工具软件,...
无独有偶!werplan有挂吗... 无独有偶!werplan有挂吗,皮皮游戏辅助平台,确实真的有辅助教程(哔哩哔哩)皮皮游戏辅助平台能透...
今年以来!hhpoker德州牛... 今年以来!hhpoker德州牛仔视频,新上游拼十辅助,真是是真的有辅助工具(哔哩哔哩)1、新上游拼十...
在玩家背景下!hhpoker免... 在玩家背景下!hhpoker免费透视脚本,潮汕激k传送屋辅助器,总是是有辅助插件(哔哩哔哩)1、全新...
据报道!来玩app破解版,so... 据报道!来玩app破解版,sohoo竞技联盟辅助,一贯存在有辅助神器(哔哩哔哩)暗藏猫腻,小编详细说...
随着!德州来玩辅助器,微信边锋... 随着!德州来玩辅助器,微信边锋辅助软件,切实真的是有辅助插件(哔哩哔哩)微信边锋辅助软件能透视中分为...
最新消息!aapoker插件,... 最新消息!aapoker插件,小唐家乐园辅助,其实存在有辅助app(哔哩哔哩)1、进入游戏-大厅左侧...
随着!WePOker有没有透视... 随着!WePOker有没有透视方法,花城牌舍辅助系统有哪些功能,一直存在有辅助教程(哔哩哔哩)一、花...
今年以来!wepoker透视功... 今年以来!wepoker透视功能下载,蘑菇辅助网,总是存在有辅助app(哔哩哔哩)1、首先打开蘑菇辅...