How to use a variable from a component in another in Angular2

前端 未结 3 1989
说谎
说谎 2020-12-31 12:09

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 12:11

    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;
        }
    }
    

    import-->Demo

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

提交回复
热议问题