【问题标题】:How to use pathlocationstrategy in angular 5 to set base href or APP_BASE_HREF?如何在角度 5 中使用 pathlocationstrategy 设置基本 href 或 APP_BASE_HREF?
【发布时间】:2018-05-25 07:09:35
【问题描述】:

下面是sn-p的代码:

进口{ 路由器 } 来自“@angular/路由器”; 进口 { HttpClient } 来自“@angular/common/http”; 进口 { 环境 } 来自“../../environments/environment”; 进口 { 地点, 位置策略, 路径定位策略 } 来自“@angular/common”;

@Injectable()
export class CommonServicesService {
    PathLocation: Location;
    referralCode: any = localStorage.getItem('referenceCode');

    constructor(
        location: Location,
    ) {
        this.PathLocation = location;
    }

    redirectAfterSuccessfulLogin() {
        if (localStorage.getItem("redirectUrl")) {
            let url = localStorage.getItem("redirectUrl");
            localStorage.removeItem("redirectUrl");
            this.PathLocation.prepareExternalUrl("'/'+this.referralCode"); //is this the correct way?
            console.log(this.PathLocation);
            this.router.navigate([url]);
        } else {
            this.PathLocation.prepareExternalUrl("'/'+this.referralCode");
            console.log(this.PathLocation);
            this.router.navigate(["/"]);
        }
    }
}

【问题讨论】:

  • 您是否在登录后尝试从一个网址导航到另一个网址?比如从 'localhost:3000/home' 到 'localhost:3000/home/code' 或者你想改变应用程序的基本 url?
  • 我想把base_href从localhost:3000改成localhost:3000/23232

标签: angular5 angularjs-ng-href


【解决方案1】:

你可以这样做:

  1. 创建一个以代码为参数的 baseUrl 函数。
  2. 登录后根据条件调用该函数,这将 为您提供更新后的网址

像这样:

getBaseUrl = function(code){
return `localhost:3000/${code}`
}

现在您可以将其用作:

getBaseURl(1234)

它返回:"localhost:3000/1234"

现在您可以在此 URL 之后添加更多路径。

您可以进一步对这两个 URL 使用相同的功能:

 getBaseUrl = function(code){

    if(code == 0000) return localhost:3000
    else return `localhost:3000/${code}`

}

现在,每当您调用该函数时,您必须传递 0000 用于非登录条件基本 URL 和 4 位代码以获取登录后基本 URL

getBaseURl(1234) // for login one

getBaseURl(0000) // for non-login one

【讨论】:

  • 好,那么getBaseUrl返回的值是怎么用的呢?
  • 通过将其与请求中的 API 路径连接起来。
  • 我认为这个问题与我们使用<base href>index.html 中设置的base_url 有关,并且没有向API 端点附加任何内容,因为这可以通过拦截器和所有内容轻松完成。跨度>
  • 我不认为是index.html的情况,它只是使用baseurl和path
猜你喜欢
  • 2020-03-24
  • 1970-01-01
  • 2018-09-28
  • 2017-03-09
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多