Angular 5 - 提交时从一个组件传递数据到另一个组件
创始人
2024-10-15 21:01:23
0

要在Angular 5中从一个组件传递数据到另一个组件,可以使用以下方法:

  1. 使用@Input和@Output装饰器: 在要传递数据的源组件中,使用@Output装饰器创建一个事件,并在需要接收数据的目标组件中使用@Input装饰器来接收数据。

在源组件中:

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

@Component({
  selector: 'source-component',
  template: `
    
  `
})
export class SourceComponent {
  @Output() dataEvent = new EventEmitter();

  sendData() {
    const data = '要传递的数据';
    this.dataEvent.emit(data);
  }
}

在目标组件中:

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

@Component({
  selector: 'target-component',
  template: `
    
接收到的数据:{{ receivedData }}
` }) export class TargetComponent { @Input() receivedData: string; }

在父组件中使用这两个组件:



在父组件的typescript文件中,定义一个变量来接收数据:

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

@Component({
  selector: 'app-parent-component',
  template: `
    
    
  `
})
export class ParentComponent {
  data: string;

  receiveData(data: string) {
    this.data = data;
  }
}
  1. 使用共享服务: 创建一个共享服务,可以在多个组件之间共享数据。在源组件中将数据存储在共享服务中,然后在目标组件中获取数据。

共享服务:

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

@Injectable()
export class DataService {
  private data: string;

  setData(data: string) {
    this.data = data;
  }

  getData(): string {
    return this.data;
  }
}

在源组件中注入共享服务,并将数据存储在共享服务中:

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

@Component({
  selector: 'source-component',
  template: `
    
  `
})
export class SourceComponent {
  constructor(private dataService: DataService) {}

  sendData() {
    const data = '要传递的数据';
    this.dataService.setData(data);
  }
}

在目标组件中注入共享服务,并获取数据:

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

@Component({
  selector: 'target-component',
  template: `
    
接收到的数据:{{ receivedData }}
` }) export class TargetComponent { receivedData: string; constructor(private dataService: DataService) {} ngOnInit() { this.receivedData = this.dataService.getData(); } }

在父组件的模板中使用这两个组件:



在父组件的typescript文件中,将共享服务添加到providers中:

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

@Component({
  selector: 'app-parent-component',
  template: `
    
    
  `,
  providers: [DataService]
})
export class ParentComponent {}

这些方法可以根据你的具体需求选择使用。

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...