您能否告诉我当服务出错时如何重定向到组件?
这是我的代码
https://stackblitz.com/edit/angular-ctwnid?file=src%2Fapp%2Ftest.service.ts
在我当前的示例中,我resolver
用来获取数据。
const routes: Routes = [
{
path: 'home',
component: HelloComponent,
resolve: { data: TestResolver }
},
{
path: 'error',
component: ErrorComponent
},
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
}
];
我改变了requested url
,所以我会得到一个错误
正确的网址:https://jsonplaceholder.typicode.com/todos/1
错误的网址:https://jsonplaceholder.typicode.com/todoss/1
如果我收到错误,我希望它重定向到Errorcomponent
.
@Injectable()
export class TestResolver implements Resolve<Observable<string>> {
constructor(private testService: TestService) {}
resolve(): Observable<any> {
return this.testService.getConfiguration();
}
}
任何更新 ?