我想從我的LandingPage.component傳遞字符串值this.title
到我的ResultPage.component。角2共享服務將數據傳遞到組件到組件
我檢索list.show
價值,它在我的發到我TitleService
在像這樣:
landingpage.component.html
<ol>
<li (click)="selectShow(list.show)" [routerLink]="['/details', list.id]" *ngFor="let list of shows">{{list.show}}
</li>
</ol>
landingpage.component.ts
import { TitleService } from '../../services/title.service';
constructor(private TitleService: TitleService) {}
selectShow(show) {
this.TitleService.fetchTitle(show)
}
以上發送list.show
值到我的:
title.service.ts
// this gives us the name of the clicked show, which we send to TitleResolver
@Injectable()
export class TitleService {
fetchTitle(title) {
console.log("title is " + title); // this outputs correctly
return title;
}
}
這裏是我如何管理我的路由:
APP-routing.module.ts
import { TitleService } from './services/title.service';
const routes: Routes = [
{ path: '', component: LandingPage },
{
path: 'details/:id', component: ResultPage
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [TitleService]
})
我的問題
有一次,我收到我的服務組件的title.show
值,我不知道該如何然後將其發送到我的接收組件(resultpage.component)
我怎樣才能把我的title
值從我的服務我的ResultPage.component?
而不是使用服務,你有沒有想過使用REDX? – garethb