我想从我的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