1)创建新项目
ng new angular-test
ng g component projects
ng g component typings
2)添加简单路由
/src/app/app.component.html
<router-outlet></router-outlet>
/src/app/app.module.ts
export const ROUTES: Routes = [
{
path: '',
redirectTo: '/projects',
pathMatch: 'full'
},
{
path: 'projects',
component: ProjectsComponent,
},
{
path: '/typings',
component: TypingsComponent
},
{
path: '**', redirectTo: ''
}
];
@NgModule({
declarations: [
AppComponent,
ProjectsComponent,
TypingsComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forChild(ROUTES)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
EXCEPTION: Error in ./AppComponent class AppComponent - inline template:3:0 caused by: No provider for RouterOutletMap!
ORIGINAL EXCEPTION: No provider for RouterOutletMap!
我尝试将RouterOutletMap添加到AppModule中的提供程序,异常不要抛出,但app不会重定向到项目并且不显示嵌套组件
答案 0 :(得分:27)
您需要为应用模块调用<?php
namespace app\views\layouts\main\assets;
use yii\web\AssetBundle;
class ThemeAsset extends AssetBundle
{
public $sourcePath = '@theme';
public $css = [
'css/site.css',
];
public $publishOptions = [
"only" => [
"css/*",
"images/*",
],
'except' => [
"doc/",
"img/",
],
"forceCopy" => YII_DEBUG,
];
}
,而不是RouterModule.forRoot
。前adds all the core providers,而the latter doesn't。您应该将forChild
用于子模块,而不是app模块。
答案 1 :(得分:0)
我也遇到过这个问题。这就是我如何避免这种情况.....有时候这会有所帮助。 祝福!
路由数组 - app.routes.ts
const appRoutes:Routes =[
{
path: 'home',
component: CarouselComponent
},
{
path: 'profile',
component:UserDashboardComponentComponent,
children: [
{
path: 'overview',
component: ProfileOverviewComponent
},
{
path: 'post',
component: PostAddComponent
}
]
}
];
export const WebRouting: ModuleWithProviders =RouterModule.forRoot(appRoutes);
<强> app.module.ts 强>
@NgModule({
declarations: [
AppComponent,
CarouselComponent,
.......
],
imports: [
BrowserModule,
MdTabsModule,
MaterialModule,
MdInputModule,
BrowserAnimationsModule,
FormsModule,
HttpModule,
WebRouting
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
<强> tempale.html 强>
<li>
<a [routerLink]="['/profile/overview']" routerLinkActive="active" >
Overview
</a>
</li>
<li>
<a [routerLink]="['/profile/post']"routerLinkActive="active">
PostAdd
</a>
</li>