我有一条路线:
{
path: ':c/:u/listview/:site',
component: SiteslistComponent,
resolve: {
apiData: ApiResolver
}
}我有一个解析器文件:
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRoute } from '@angular/router';
import { ApiService } from './api.service';
@Injectable()
export class ApiResolver implements Resolve<any> {
public _customer;
public _userId;
constructor(
private apiService:ApiService,
private route:ActivatedRoute
) {
let hash = window.location.hash;
let params = hash.split('/');
this._customer = params[1];
this._userId = params[2];// <<< USE THIS WHEN DEPLOYING TO PRODUCTION
}
resolve() {
if (isNaN(this._userId)) {
this._userId = 14152;
}
return this.apiService.getSites(this._customer, this._userId);
}
}我需要添加一个新的路线到我的应用程序,也使用一个解析器。
{
path: ':c/:u/siteoverview/:siteid/overview',
component: SiteOverviewComponent,
resolve: {
apiData2: ApiResolver
}
}但我很困惑。我是否可以使用相同的解析器文件并以某种方式添加第二个解析函数?还是需要为我需要创建的每个解析函数创建一个完整的独立解析器文件?
发布于 2020-04-30 03:43:04
您可以在路由配置中使用data选项将某些内容发送到解决函数。此外,这将用于区分api调用。
{
path: ':c/:u/siteoverview/:siteid/overview',
component: SiteOverviewComponent,
resolve: {
apiData2: ApiResolver
},
data: {
resolveMethod: 'getSites'
}
},
{
path: ':c/:u/siteoverview/:siteid/overview',
component: SiteOverviewComponent,
resolve: {
apiData2: ApiResolver
},
data: {
resolveMethod: 'getUsers'
}
}resolver.ts
@Injectable()
export class ApiResolver implements Resolve<any> {
...
resolve(route: ActivatedRouteSnapshot) {
const method = route.data['resolveMethod'];
return this[method]();
}
getSites() {
if (isNaN(this._userId)) {
this._userId = 14152;
}
return this.apiService.getSites(this._customer, this._userId);
}
getUsers() {
...
}
}https://stackoverflow.com/questions/61515458
复制相似问题