Angular 2 shared service to pass data to component-to-component

后端 未结 3 468
南旧
南旧 2021-01-16 12:20

I am trying to pass the string value of this.title from my LandingPage.component to my ResultPage.component.

I retrieve the list.show value

3条回答
  •  长情又很酷
    2021-01-16 12:58

    In title.service.ts you can declare a variable called title and have setter and getter:

    title: string ="";
    
    // replace fetchTitle with setTitle 
    // remember to change it in the component too
    setTitle(title) {
        this.title = title;
      }
    
    getTitle() {
        return this.title;
      }
    

    Then, when ResultPage.component is initialized, call getTitle() from TitleService and set the result to a variable declared in the component.

    Here's an example of sharing data via shared services.

提交回复
热议问题