是否可以让一些页面使用 PathLocationStrategy 而一些页面使用 HashLocationStrategy?
例如,登录名不会有任何哈希值,其余页面的 URL 中会有哈希值。
目前我没有看到一个简单的方法来做到这一点。它是所有带有或不带有散列的全部,如下例所示:
@NgModule({
imports: [
RouterModule.forRoot(LAYOUT_ROUTES, { useHash: false , enableTracing: DEBUG_INFO_ENABLED })
],
exports: [
RouterModule
]
})
谢谢
最佳答案
我在这里找到了我要找的东西:Angular2 - APP_BASE_HREF with HashLocationStrategy
我使用了 CustomLocationStrategy 并根据需要更改了 URL
import { Injectable } from '@angular/core';
import { HashLocationStrategy } from '@angular/common';
@Injectable()
export class CustomLocationStrategy extends HashLocationStrategy {
prepareExternalUrl(internal: string): string {
let url = this.getBaseHref() + '/#' + internal;
if (internal === '/login') {
url = this.getBaseHref() + internal;
}
return url;
}
}
和 index.html
...
<base href="./" />
...
关于Angular hash 和 path 策略一起——混合模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49896018/