当我为我的应用程序编写路由时,会弹出此错误
Type '({ path: string; redirectTo: string; pathMatch: string; } | { path: string; component: typeof Rec...' is not assignable to type 'Route[]
我的路线档案是
import {RouterModule, Routes} from '@angular/router';
import {RecipesComponent} from './recipes/recipes.component';
import {ShoppingListComponent} from './shopping-list/shopping-list.component';
import {recipe_routing} from "./recipes/recipe.routes";
const approutes: Routes = [
{path: '', redirectTo: '/recipes', pathMatch: 'full'},
{path: 'recipes', component: RecipesComponent, children: recipe_routing},
{path: 'shopping-list', component: ShoppingListComponent}
];
export const routing = RouterModule.forRoot(approutes);
我在这个文件中做了什么错误它没有工作。它说approutes(我指定的常量)是那个不起作用的。
食谱路线
import {Routes} from "@angular/router";
import {RecipeStartComponent} from "./recipe-start.component";
import {RecipeEditComponent} from "./recipe-edit/recipe-edit.component";
import {RecipeDetailComponent} from "./recipe-detail/recipe-detail.component";
export const recipe_routing: Routes = [
{path: '', component: RecipeStartComponent},
{path: 'new', component: RecipeEditComponent},
{path: ':id', component: RecipeDetailComponent},
{path: ':id/edit', component: RecipeEditComponent}
];
这是食谱路线
我没有写export const routing = RouterModule.forRoot(approutes);
,因为这只是一条儿童路线。
答案 0 :(得分:1)
我没有写出口const routing = RouterModule.forRoot(approutes);因为这个只是一个子路径。
对于子路线,您应该使用forChild()
方法,如下所示:
const approutes: Routes[] = [...]
export const routing = RouterModule.forChild(approutes)
...
@NgModule({
imports: [routing]
...
)
答案 1 :(得分:0)
您没有在单个语句中导出常量。即。 export const approutes
或许试试这种方式。
RouterModule.forRoot(approutes)应该在导入内部的@ngModule中的app-module.ts中:[]