Jar*_*rek 7 angular-routing angular
我有一个角度应用程序使用路由与HashLocationStrategy,我需要在主html文件中设置不同的值和路由不同.
我试过这个解决方案:
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
MyRouting // with useHash set to true
],
declarations: [
AppComponent,
],
providers: [
{ provide: APP_BASE_HREF, useValue: '/prefix' }
],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
工作得很好,但值'/ prefix'是在哈希后插入,如下所示:
http://myapp.com/#/prefix/home
Run Code Online (Sandbox Code Playgroud)
我想要的是:
http://myapp.com/prefix/#/home
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我的基本标签是:
<base href="/">
Run Code Online (Sandbox Code Playgroud)
Vol*_*res 10
我遇到了同样的问题,用我自己的HashLocationStrategy子类修复了它
import { Injectable } from '@angular/core';
import { HashLocationStrategy } from '@angular/common';
@Injectable()
export class CustomLocationStrategy extends HashLocationStrategy {
prepareExternalUrl(internal: string): string {
return this.getBaseHref() + '#' + internal;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的模块中使用它
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LocationStrategy } from '@angular/common';
import { APP_BASE_HREF } from '@angular/common';
import { CustomLocationStrategy } from './app.common';
const appRoutes: Routes = [...];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes, { useHash: true })
],
providers: [
{ provide: APP_BASE_HREF, useValue: window.location.pathname },
{ provide: LocationStrategy, useClass: CustomLocationStrategy },
]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5968 次 |
| 最近记录: |