【问题标题】:Angular2 keep/add trailing slashAngular2保留/添加斜杠
【发布时间】:2016-11-28 09:00:45
【问题描述】:

我遇到了 Angular2 删除尾部斜杠的问题。我已经设置了我的 dotnet 核心应用程序来添加它们,但是一旦加载了 js,它们就会被删除。

在 Angular2 中甚至可能吗?

(我的客户需要它,所以不需要添加评论说;不要使用斜杠)。

提前致谢。

编辑: 应该添加,这是与 Angular2-Universal 一起运行的,所以升级 Angular 版本并不容易:(

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    我们有一个 Angular 的网站,在我们决定迁移到 Angular4 Universal 之后,我们的一个问题是自动删除尾随斜杠对我们的网站 SEO 的影响,经过多次尝试,我们找到了一种防止这种无价值行为的骇人听闻的方法。如下代码(在 app.module.ts 文件中):

    import { NgModule } from '@angular/core';
    import { Injectable } from '@angular/core';
    import { Location } from '@angular/common';
    
    import {AppComponent} from './app.component';
    
    
    @Injectable()
    export class UnstripTrailingSlashLocation extends Location {
      public static stripTrailingSlash(url: string): string {
        return url;
      }
    }
    
    
    Location.stripTrailingSlash = UnstripTrailingSlashLocation.stripTrailingSlash;
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [...],
      providers: [...],
      bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    

    【讨论】:

      【解决方案2】:

      我相信是 .Net Devs 的 problem faced

      2.3.0-beta.1 版本的 angular 中进行此修复的提交。尝试更改您的角度版本,看看问题是否已解决。

      fix: support trailing slash in basePath

      或者

      你试过了吗?这似乎可以在我的机器上解决问题

      在 app.routing.ts 中:

      { path: 'link/.', component: MyComponent },

      超链接:

      <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
              <a class="nav-link" [routerLink]="['/link/.']">MyComponent</a>
          </li>
      

      【讨论】:

      • 这似乎只适用于basePath。如果我转到“/articles/article-1/”,它会将其剥离为“articles/article-1” - 但我需要并且希望它保留尾部斜杠。
      • 在斜线后加句点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2012-01-12
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      相关资源
      最近更新 更多