我正在使用Angular2开发一个webapp。我正在尝试使用HasLocationStratery实现路由。我的应用程序以此网址开头: https://127.0.0.1:8443/admin
这是我配置路线的方式:
import {Routes, RouterModule} from '@angular/router';
import {AppComponentAdmin} from "./app.component.admin";
import {ModuleWithProviders} from "@angular/core";
import {MainComponent} from "./administrator/components/main/main.component";
import {LoginComponent} from "./administrator/components/account/login/login.component";
import {WorkorderComponent} from "./administrator/components/entities/workorder/workorder.component";
import {WorkorderViewEditComponent} from "./administrator/components/entities/workorder/workorder.view.edit.component";
const appRoutes: Routes = [
//admin paths
{ path: 'admin', component: AppComponentAdmin},
{ path: 'admin/login', component: MainComponent },
{ path: 'admin/creds', component: LoginComponent },
{ path: 'admin/workorder', component: WorkorderComponent },
{ path: 'admin/workorder/:id', component: WorkorderViewEditComponent },
// otherwise redirect to admin
{ path: 'admin/**', redirectTo: 'admin', pathMatch: 'full' }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
在我导入的模块中。
import {HashLocationStrategy, LocationStrategy} from "@angular/common"
并在模块
中提供[{provide: LocationStrategy, useClass: HashLocationStrategy}]
现在,当我访问 https://127.0.0.1:8443/admin
时我在地址栏 https://127.0.0.1:8443/#/
中得到此信息它应该给我的是 https://127.0.0.1:8443/admin#/
我怎样才能实现这个目标?
答案 0 :(得分:1)
要使用子目录配置路由,您必须根据物理目录定义基本href。
例如,在您的情况下,它应该是<base href="/admin">