简体   繁体   English

在angular2中需要将数据从一个组件发送到另一组件

[英]In angular2 need to send data from one component to another component

After entering mobile no from login component to verify the otp from otp component i need that mobile no and country code that user entered to pass as parameters to api in otp service. 在从登录组件输入手机号以验证otp组件的otp之后,我需要该手机号和用户输入的国家/地区代码作为参数传递给otp服务中的api。 I need to pass data from login to otp component how could i perform that in angular2 ? 我需要将数据从登录名传递到otp组件,我该如何在angular2中执行该操作?

Sandip is right. Sandip是正确的。 For example define the following shared variable inside the 'SharedDataService': 例如,在“ SharedDataService”中定义以下共享变量:

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

@Injectable()
export class SharedDataService{
    hasUserLoggedIn: boolean;
}

Then use this service inside say 'AppComponent': 然后在“ AppComponent”中使用此服务:

@Component({
  providers: [ SharedDataService ]
})

the variable then can be used across the whole app. 然后可以在整个应用中使用该变量。

Option 1 : You can use Shared Services 选项1:您可以使用共享服务

Option 2 : Use Pub-Sub way , so that can be used anywhere without injecting a service instance 选项2:使用Pub-Sub方式,因此可以在任何地方使用而无需注入服务实例

Option 3 (Which I strongly suggest to use ): Use Redux 选项3(强烈建议使用):使用Redux

Which will help to maintain the app state . 这将有助于维持应用程序状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM