Angular 5 : 允许在路由中使用尾部斜线
Angular 5 : Allow trailing slash in routes
我正在用 Angular 更新一个旧网站,我必须满足的要求之一是所有路由都应保持原样(出于 SEO 目的)。
许多旧网站的路由以斜杠结尾(如 /my/route/
),其中一些以 .html
结尾,如 /my/route.html
.
问题是 routerLink 删除了每条以斜杠结尾的路由中的最后一个斜杠(我的路由现在是 /my/route
)。
如何使 routerLink
保留结尾的斜杠?
这里可以看到一个简单的例子:AngularTrailingSlash。
我在下面添加到你的 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
不存在时,它对我来说可以用尾部斜杠
在 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
尝试将位置 class 扩展到 AppModule
,如下所示:
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({
...
更多改进的代码(基于 Umesh 的代码)。
在main.ts
之上添加:
import { Location } from "@angular/common";
// Prevent stripping of trailing slash
Location.stripTrailingSlash = function stripTrailingSlashNew(url: string) {
return url;
};
以下代码对我有用:
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);
};
我在下面提到了这个解决方案 link:
https://stackblitz.com/github/ssatz/Angular5-Preserve-Trailing-Slash
我正在用 Angular 更新一个旧网站,我必须满足的要求之一是所有路由都应保持原样(出于 SEO 目的)。
许多旧网站的路由以斜杠结尾(如 /my/route/
),其中一些以 .html
结尾,如 /my/route.html
.
问题是 routerLink 删除了每条以斜杠结尾的路由中的最后一个斜杠(我的路由现在是 /my/route
)。
如何使 routerLink
保留结尾的斜杠?
这里可以看到一个简单的例子:AngularTrailingSlash。
我在下面添加到你的 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
不存在时,它对我来说可以用尾部斜杠
在 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
尝试将位置 class 扩展到 AppModule
,如下所示:
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({
...
更多改进的代码(基于 Umesh 的代码)。
在main.ts
之上添加:
import { Location } from "@angular/common";
// Prevent stripping of trailing slash
Location.stripTrailingSlash = function stripTrailingSlashNew(url: string) {
return url;
};
以下代码对我有用:
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);
};
我在下面提到了这个解决方案 link:
https://stackblitz.com/github/ssatz/Angular5-Preserve-Trailing-Slash