键入 '({ 路径: 字符串; 重定向到: 字符串; 路径匹配: 字符串; } |{ 路径:字符串;组件:类型Rec..."不可分配给类型"路由[]



当我为应用程序编写路由时弹出此错误

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);,因为这只是一条儿童路线。

我没有写导出常量路由= RouterModule.forRoot(approutes(;因为这只是一条儿童路线

对于子路由,应使用如下forChild()方法:

const approutes: Routes[] = [...]
export const routing = RouterModule.forChild(approutes)
...
@NgModule({
imports: [routing]
...
)

您没有在单个语句中导出常量。export const approutes也许可以尝试一下。 RouterModule.forRoot(approutes( 应该在 app-module.ts 的 @ngModule 导入中:[]

最新更新