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

后端 未结 3 402
南旧
南旧 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 13:11

    Make the title a public property of the service like this:

    // this gives us the name of the clicked show, which we send to TitleResolver
    @Injectable()
    export class TitleService {
      selectedTitle: string;
    
      fetchTitle(title) {
        console.log("title is " + title); // this outputs correctly
        this.selectedTitle = title;
        return title;   // No need to return it.
      }
    }
    

    Then any other component can inject this service and access this.titleService.selectedTitle

提交回复
热议问题