【发布时间】:2017-11-20 11:38:20
【问题描述】:
我正在尝试在 Angular 4 应用程序中使用 Angular 路由,但该应用程序无法加载与请求的路由匹配的组件:
这里是app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module.ts 看起来像这样:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
declarations: [
AppComponent,
DashboardComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
],
providers: [ ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.component.html:
<h1>
Welcome to {{title}}!
</h1>
<router-outlet></router-outlet>
我的dashboard.component.html:
<p>dashboard works!</p>
我试图找出问题所在,还查看了 Angular 4 Routing tutorial。请帮忙。
【问题讨论】:
-
import { AppRoutingModule } from './/app-routing.module'; Loot at ".//app.." 为什么是两个斜线?
-
此导入由
ng generate在 app.module.ts 中添加。但是,它在删除一个斜线后尝试,没有工作。组件“AppComponent”未加载。 -
您遇到的错误是什么?
-
我查看了浏览器控制台,上面写着:
Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document. at new PathLocationStrategy (common.js:647) at provideLocationStrategy (router.js:6995) at _callFactory (core.js:10651) at _createProviderInstance$1 (core.js:10599) at initNgModule (core.js:10549) at new NgModuleRef_ (core.js:11792) at createNgModuleRef (core.js:11782) at Object.debugCreateNgModuleRef [as createNgModuleRef] (core.js:14092) at NgModuleFactory_.create (core.js:15216) at eval (core.js:5370)