如何在Angular2的另一个组件中使用组件中的变量 [英] How to use a variable from a component in another in Angular2

查看:171
本文介绍了如何在Angular2的另一个组件中使用组件中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在不使用第一个组件的模板的情况下,将一个组件中的变量值使用到另一个组件中,只需要其他变量的值即可.有可能吗?

I was wondering if it is possible to use variable values from one component into another one without having to use the template of the first one, just need the value of the variable nothing else. Is it possible?

推荐答案

是的,您可以使用@Input()方法或使用如下所示的write get方法

Yes possible, you can use the @Input() method or use write get method like below

export class Demo {
    const sum = 10;

    get SumValue() {
        return this.sum;
    }
}


导入->演示

export class Demo2 {
   private sum: number;
   this.sum = Demo.SumValue();
}

这篇关于如何在Angular2的另一个组件中使用组件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆