带有 resolve 方法参数的解析器
resolver with parameter on resolve method
所以我在 angular 5:
中实现了一个解析器
@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> {
constructor(private myService: MyService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<MyComplexObject[]> {
return this.myService.getMyApi(myOption); // my option is a string
}
}
现在 myOption 是硬编码字符串,我想更改它
在我的路由模块中我有:
resolve: {
myResolver: AppResolver
}
我想也许我应该在这里指定 myOption 字符串的值,但是如何指定呢?
或者更好的是我实际调用解析器的地方
this.route.data.map(data => data.myResolver).subscribe(
result => {
// do something with the result (specify who the myOption is?? How)
}
);
该参数不一定在浏览器中可见:
它将成为 url 的一部分:/.../.../myString/..
但它不是由参数引入的:url: /..&myParam=paramValue
所以我无法使用 myParam 从 url 中识别它并替换它
这里是发送 data
到 resolver
,
的例子
路由配置:
{
path: 'project/:id',
component: ProjectComponent,
resolve: { data: AppResolver },
data: { resolvedata: 'myValue' }
}
解析器:
@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> {
constructor(private myService: MyService, private router: Router) {}
resolve(route: ActivatedRouteSnapshot): Observable<MyComplexObject[]>|boolean {
let myParam = route.data['resolvedata'];
console.log(myParam);
}
}
所以我在 angular 5:
中实现了一个解析器@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> {
constructor(private myService: MyService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<MyComplexObject[]> {
return this.myService.getMyApi(myOption); // my option is a string
}
}
现在 myOption 是硬编码字符串,我想更改它
在我的路由模块中我有:
resolve: {
myResolver: AppResolver
}
我想也许我应该在这里指定 myOption 字符串的值,但是如何指定呢?
或者更好的是我实际调用解析器的地方
this.route.data.map(data => data.myResolver).subscribe(
result => {
// do something with the result (specify who the myOption is?? How)
}
);
该参数不一定在浏览器中可见:
它将成为 url 的一部分:/.../.../myString/..
但它不是由参数引入的:url: /..&myParam=paramValue
所以我无法使用 myParam 从 url 中识别它并替换它
这里是发送 data
到 resolver
,
路由配置:
{
path: 'project/:id',
component: ProjectComponent,
resolve: { data: AppResolver },
data: { resolvedata: 'myValue' }
}
解析器:
@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> {
constructor(private myService: MyService, private router: Router) {}
resolve(route: ActivatedRouteSnapshot): Observable<MyComplexObject[]>|boolean {
let myParam = route.data['resolvedata'];
console.log(myParam);
}
}