解析器上带有参数的解析器 [英] resolver with parameter on resolve method

查看:86
本文介绍了解析器上带有参数的解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在角度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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆