解析器上带有参数的解析器 [英] resolver with parameter on resolve method
问题描述
所以我在角度5中实现了一个解析器:
So I implemented a resolver in 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是一个硬编码的字符串,我想更改它
right now myOption is a hardcoded string and i want to change that
在我的路由模块中,我有:
in my routing module i have:
resolve: {
myResolver: AppResolver
}
我想也许我应该在这里指定myOption字符串的值,但是如何?
I suppose maybe here I should specify the value of myOption string but how?
或更妙的是,我实际在哪里称呼解析器
or better yet where I actually call the resolver
this.route.data.map(data => data.myResolver).subscribe(
result => {
// do something with the result (specify who the myOption is?? How)
}
);
该参数不一定在浏览器中可见:
the parameter is not necessarily visible in the browser :
它将是网址的一部分:/.../.../myString/..
it will be part of the url: /.../.../myString/..
但是它不是由param引入的:url:/..& amp; myParam = paramValue
but it's not introduced by a param : url: /..&myParam=paramValue
所以我不能使用myParam从URL中识别它并替换它
so I can t use myParam to identify it from the url and replace it
推荐答案
以下是将data
发送到resolver
的示例,
Here is an example to send data
to 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);
}
}
这篇关于解析器上带有参数的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!