How can I create a variable in a service that gets its data from a promise yet is shared between two components?

后端 未结 3 348
天涯浪人
天涯浪人 2021-01-15 19:41

I have a service in an Angular 2 using TypeScript. I want to be able to share an array of values that I get from that service. when one component makes a change to the array

3条回答
  •  礼貌的吻别
    2021-01-15 20:19

    In your component

    //set favorites
        getFavorites(): void{
            this.favoriteService
                .getFavorites()
                .then(favorites => this.favoriteService.favorites = favorites);
        }  
    
    // get favorite
    this.favorites = this.favoriteService.favorites;//access it from service
    

提交回复
热议问题