Angular 5:允许在路径中使用尾随斜杠

时间:2018-01-24 14:41:06

标签: angular routing angular5

我正在使用Angular更新旧网站,我必须满足的一个要求是所有路由应保持原样(出于搜索引擎优化目的)。

许多旧网站的路线都以斜线(如/my/route/)结束,其中一些路线以.html结束,如/my/route.html

问题是routerLink删除了每条路线中的最后一个斜线(斜线现在为/my/route)。

如何让routerLink保留尾随斜杠?

这里可以看到一个简单的例子:AngularTrailingSlash

5 个答案:

答案 0 :(得分:2)

我在下面添加了app.module.ts

import {Location, PathLocationStrategy} from '@angular/common';


const _orig_prepareExternalUrl = PathLocationStrategy.prototype.prepareExternalUrl;

PathLocationStrategy.prototype.prepareExternalUrl = function(internal) {
  const url = _orig_prepareExternalUrl.call(this, internal);

  if (url === '') {
    return url;
  }

  console.log('For ' + internal + ' we generated ' + url);
  if (url.endsWith('.html')) {
      return url;
  }

  if (url.endsWith('/')) {
    return url;
  }


  return url + '/';

};

Location.stripTrailingSlash = function (url) {
    const /** @type {?} */ match = url.match(/#|\?|$/);
    const /** @type {?} */ pathEndIdx = match && match.index || url.length;
    const /** @type {?} */ droppedSlashIdx = pathEndIdx - (url[pathEndIdx - 1] === '/' ? 1 : 0);
    const first = url.slice(0, droppedSlashIdx);
    const last = url.slice(pathEndIdx);

    if (first.endsWith('.html')) {
        return first + last;
    }

    return first + '/' + last;

};

.html不存在

时,它现在可以使用尾部斜杠

答案 1 :(得分:2)

以下代码对我有用:

export const contactUsRoutes: Routes = [
    {
        path: 'contact-us/.', component: ContactUsComponent
    }
];

<a [routerLink]="['/contact-us/.']">Contact us</a>

并在main.ts中添加以下代码:

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

const __stripTrailingSlash = (Location as any).stripTrailingSlash;
(Location as any).stripTrailingSlash = function _stripTrailingSlash(url: string): string {
  const queryString$ = url.match(/([^?]*)?(.*)/);
  if (queryString$[2].length > 0) {
    return /[^\/]\/$/.test(queryString$[1]) ? queryString$[1] + '.' + queryString$[2] : __stripTrailingSlash(url);
  }
  return /[^\/]\/$/.test(url) ? url + '.' : __stripTrailingSlash(url);
};

我已通过以下链接获得此解决方案:

https://stackblitz.com/github/ssatz/Angular5-Preserve-Trailing-Slash

答案 2 :(得分:1)

甚至还有改进的代码(基于Umesh的代码)。

main.ts顶部添加:

import { Location } from "@angular/common";
// Prevent stripping of trailing slash
Location.stripTrailingSlash = function stripTrailingSlashNew(url: string) {
  return url;
};

答案 3 :(得分:0)

在main.ts文件中添加此代码,以在routerLink中保留尾随斜杠。

import { Location } from '@angular/common';
const __stripTrailingSlash = (Location as any).stripTrailingSlash;
(Location as any).stripTrailingSlash = function _stripTrailingSlash(url: string): string {
  return /[^\/]\/$/.test(url) ? url : __stripTrailingSlash(url);
}

// import main ng module
// bootstrap on document ready

答案 4 :(得分:0)

尝试使用此类AppModule中的Location类扩展:

app.module.ts:

import { Injectable } from '@angular/core';
import { Location } from '@angular/common';

@Injectable()
export class UnstripTrailingSlashLocation extends Location {
  public static stripTrailingSlash(url: string): string {
    return url;
  }
}
Location.stripTrailingSlash = UnstripTrailingSlashLocation.stripTrailingSlash;

@NgModule({
            ...