【发布时间】:2018-08-15 01:09:15
【问题描述】:
我在 Angular 5 项目中遇到了这个问题。所有使用 localhost 的 URL,甚至在托管时,在域名后跟一个“#”。找不到原因。
app-routing.module.ts 其他组件有子路由
const appRoutes: Routes = [
{ path: 'login', component: LoginComponent},
{ path: 'new-vendor-registration', component: NewVendorComponent},
{ path: 'new-vendor-card-add', component: NewVendorCardComponent },
{ path: 'forgot-password', component: ForgotPasswordComponent},
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full',
},
{ path: '', component: DashboardLayoutComponent, data: { title: '' }, children: DASHBOARD_ROUTES },
{ path: '', component: DashboardLayoutComponent, data: { title: '' }, children: USER_ROUTES, canActivate: [AuthGuard] },
{ path: '', component: DashboardLayoutComponent, data: { title: '' }, children: OFFER_ROUTES, canActivate: [AuthGuard] },
{ path: '', component: DashboardLayoutComponent, data: { title: '' }, children: PROMOTION_ROUTES, canActivate: [AuthGuard] }
// { path: '', component: TwoColumnsLayoutComponent, data: { title: '' }, children: TWO_COLUMN_ROUTES, canActivate: [AuthGuard] },
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
app.module.ts
@NgModule({
declarations: [
AppComponent,
TwoColumnsLayoutComponent,
DashboardLayoutComponent,
LoginComponent,
NewVendorComponent,
NewVendorCardComponent,
ForgotPasswordComponent
],
imports: [
BrowserAnimationsModule,
NgxDatatableModule,
HttpClientModule,
FormsModule,
AppRoutingModule,
SharedModule,
NgbModule.forRoot(),
AngularFireModule.initializeApp(firebaseConfig),
AngularFireDatabaseModule,
AngularFireAuthModule,
MyDatePickerModule,
TextMaskModule
],
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy},
AuthService,
AuthGuard,
VendorService,
SalesrepService,
PaymentService,
SubscriptionService,
OfferService,
PromotionService,
WeatherService,
Broadcaster
],
bootstrap: [AppComponent]
})
export class AppModule {}
非常感谢任何帮助。
【问题讨论】:
-
angular.io/api/common/LocationStrategy 显示你的 app.module.ts
-
你能显示你的
navbarhtml 代码吗?我猜你的anchor标签href="#"正在制造问题。将其更改为“javascript:void(0)” -
在 index.html 中验证您的基本路径,它应该类似于
没有哈希。 -
我用 app.module.ts @ngmodule 更新了帖子。基本 href 设置为“/”。还是一样