2017-07-19 34 views
1

此錯誤在我彈出時寫路線我的應用程序類型'({path:string; redirectTo:string; pathMatch:string;} | {path:string; component:typeof Rec ...'is not assignable to type'Route []

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); 

什麼錯誤,我是用這個文件它不是working.It做說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

嘗試這樣'常量approutes:路線[] = [...]' –

+0

可您發佈'recipe_routing'?這個問題很可能存在 – PierreDuc

+0

我已經包含了它,而Maximus的工作方式不是這樣,一個錯誤說類型'ModuleWithProviders'不能分配類型'Route []'。彈出 – Mannish

回答

0

您沒有在單個語句中導出常量。即。 export const approutes也許嘗試這種方式。 RouterModule.forRoot(approutes)應該是在APP-module.ts在進口內的@ngModule:[]

1

我沒有寫出口const的路由= RouterModule.forRoot(approutes);自這只是一條兒童路線

對於孩子的路線,你應該使用forChild()方法是這樣的:

const approutes: Routes[] = [...] 

export const routing = RouterModule.forChild(approutes) 

... 

@NgModule({ 
    imports: [routing] 
    ... 
)