首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一个解析器文件中可以有两个单独的API调用吗?(角9)

在一个解析器文件中可以有两个单独的API调用吗?(角9)
EN

Stack Overflow用户
提问于 2020-04-30 03:30:57
回答 1查看 222关注 0票数 0

我有一条路线:

代码语言:javascript
复制
{
    path: ':c/:u/listview/:site',
    component: SiteslistComponent,
    resolve: {
        apiData: ApiResolver
    }
}

我有一个解析器文件:

代码语言:javascript
复制
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);
    }


}

我需要添加一个新的路线到我的应用程序,也使用一个解析器。

代码语言:javascript
复制
{
    path: ':c/:u/siteoverview/:siteid/overview',
    component: SiteOverviewComponent,
    resolve: {
        apiData2: ApiResolver
    }
}

但我很困惑。我是否可以使用相同的解析器文件并以某种方式添加第二个解析函数?还是需要为我需要创建的每个解析函数创建一个完整的独立解析器文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-30 03:43:04

您可以在路由配置中使用data选项将某些内容发送到解决函数。此外,这将用于区分api调用。

代码语言:javascript
复制
{
    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

代码语言:javascript
复制
@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() {
    ...
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61515458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档