更改角度导航的基本href

时间:2018-12-05 04:29:20

标签: angular angular-routing angular-router

在使用 ng build 构建我的角度应用程序时,我为应用程序提供了-base-href 标志,即 foo 。因此,我的应用程序的URL看起来像https://web.archive.org/web/20100222010428/http://wiki.ada-dk.org/index.php/Ada.Containers.Vectors#Vectors.Generic_Sorting。但是,我想导航到URL中带有栏而不是https://10.10.10.19:8080/foo的页面。

我尝试了router.navigateByUrl('/ bar'),但是它创建的URL https://10.10.10.19:8080/bar并不是我想要的。

有人可以帮我解决这里丢失或出现的问题吗?

3 个答案:

答案 0 :(得分:0)

可以通过依赖注入或通过httpInterceptors来实现。

希望下面的链接对您的问题有所帮助。 http://www.projectcodify.com/angular-set-base-url-dynamically

答案 1 :(得分:0)

不确定在构建之后,但是在开发过程中我使用以下代码及其对我有用。.

this.router.navigate(['../bar'], { relativeTo: this.route });

答案 2 :(得分:0)

基本上,您希望动态设置基本href,因为您不能仅删除基本href(AFAIK)。

在main.ts上导入base_HREF

import { APP_BASE_HREF } from '@angular/common';

然后将您的提供商设置为

providers: [
    {
        provide: APP_BASE_HREF,
        useValue: '/' + (window.location.pathname.split('/')[1] || '')
    }
]

信用此链接:此链接可以使用:Angular 2 / 4 / 5 - Set base href dynamically