简体   繁体   English

如何在Angular2中将变量值从一个组件传递到另一个组件

[英]How to pass variable value from one component to another in Angular2

I am rebuilding an old web platform (coded in php) using Angular4 and angular-cli. 我正在使用Angular4和angular-cli重建一个旧的Web平台(用php编码)。

I'm looking for a way to declare multiple variables once in a component called my-configuration so that I can use them directly in other components, such as my-footer, without redeclaring them: 我正在寻找一种在名为my-configuration的组件中声明多个变量一次的方法,以便我可以直接在其他组件中使用它们,例如my-footer,而无需重新声明它们:

my-configuration.component.ts 我-configuration.component.ts

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

@Component ({
  selector: 'my-configuration',
  templateUrl: './my-configuration.component.html',
  styleUrls: ['./my-configuration.component.css']
})

export class MyConfigurationComponent {
    variable1: String;
    variable2: String;

    constructor(){
        this.variable1 = 'value1';
        this.variable2 = 'value2';
    }
}

my-footer.component.ts 我-footer.component.ts

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

@Component ({
  selector: 'my-footer',
  templateUrl: './my-footer.component.html',
  styleUrls: ['./my-footer.component.css']
})

export class MyFooterComponent {
    footer-variable1: String;
    footer-variable2: String;

    constructor(){
        this.footer-variable1 = //Get the variable1 value from MyConfigurationComponent;
        this.footer-variable1 = //Get the variable2 value from MyConfigurationComponent;
    }
}

What is the correct method to do so? 这样做的正确方法是什么?

The recommended way to share data throughout an application is to use an Angular service. 在整个应用程序中共享数据的推荐方法是使用Angular服务。 They are defined as singletons, so any values you retain there are available to the entire application. 它们被定义为单例,因此您保留的任何值都可用于整个应用程序。

You can find out more about services here: https://angular.io/docs/ts/latest/tutorial/toh-pt4.html 您可以在此处找到有关服务的更多信息: https//angular.io/docs/ts/latest/tutorial/toh-pt4.html

I just put together a blog post and a plunker showing how to do this: 我刚刚整理了一篇博文和一篇文章,展示了如何做到这一点:

http://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/ http://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

enter code here

https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview

look in environments/environment.ts , you can put your variable there, or you can use like this 看看环境/ environment.ts,你可以把你的变量放在那里,或者你可以像这样使用

 export const config = {
  variable1: 'ble',
  variable2: 'bla'
};

if your two component have parent and child relation ship you can use @ViewChild() or @Output() otherwise Service is the best way to share the data. 如果你的两个组件有父子关系,你可以使用@ViewChild()或@Output(),否则服务是共享数据的最佳方式。

you can find more information about component communication https://angular.io/docs/ts/latest/cookbook/component-communication.html 您可以找到有关组件通信的更多信息https://angular.io/docs/ts/latest/cookbook/component-communication.html

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

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