我需要允许我的应用程序的用户在他们想要的时候更改默认路由:我有一个参数页面,他们可以在登录时选择他们想要首先显示的"页面".
例如,当他们登录时,他们会重定向到Day,但是他们希望能够更改它并在他们登录时的周或月重定向.
{ path: 'planification', component: PlanificationComponent,
children: [
{ path: '', redirectTo: 'Day', pathMatch: 'full' },
{ path: 'day', component: DayComponent },
{ path: 'week', component: WeekComponent },
{ path: 'month', component: MonthComponent }]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
@ angular/core:2.4.7
@ angular/router:3.4.7
谢谢你的帮助!
我创建了一个服务,以便从API获取数据并使用Subject从我的组件中获取这些数据(在应用程序初始化时调用LoadTenants):
服务 :
@Injectable()
export class TenantService{
public tenants;
private tenantSubject = new Subject<any>();
constructor(private http: HttpClient) {
}
LoadTenants() {
this.http.get(TENANT_URL).subscribe((data: any) => {
this.tenants = data;
this.setTenants();
});
}
setTenants() {
this.tenantSubject.next(this.tenants);
}
getTenants(): Observable<any> {
return this.tenantSubject.asObservable();
}
getSyncTenants() {
return this.tenants;
}
}
Run Code Online (Sandbox Code Playgroud)
组成部分:
public tenant;
subsciption: Subscription;
constructor(private tenantService: TenantService) { }
ngOnInit() {
this.subsciption = this.tenantService.getTenants().subscribe((datas) => {
this.tenant = datas;
},
error => console.log('Error : ', error)
);
}
}
ngOnDestroy() {
this.subsciption.unsubscribe(); …Run Code Online (Sandbox Code Playgroud)