我一直在尝试创建一个基于cli的示例angular4应用程序,其中包含一个主模块和3个产品模块(产品本身就是一个路径参数,可以懒洋洋地加载每个产品屏幕)。
以下是我的示例 - https://github.com/shankarvn/angular4lazyloading
重现的步骤
git clone https://github.com/shankarvn/angular4lazyloading.git
cd application
npm install
ng serve -p 4003
在浏览器中localhost:4003 =>应该加载3张显示product1,product2和product3的卡片。此时,单击product1,您将看到路径更改和product1加载的ui。现在单击仪表板,您将在控制台中看到错误
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'product1/dashboard'
Error: Cannot match any routes. URL Segment: 'product1/dashboard'
at ApplyRedirects.noMatchError (router.es5.js:1404) [angular]
at CatchSubscriber.selector (router.es5.js:1379) [angular]
at CatchSubscriber.error (catch.js:104) [angular]
不确定我做错了什么 - 只是在延迟加载product1模块时加载仪表板路径。加载product1模块时,不应该注册路由。任何帮助表示赞赏。
感谢。
答案 0 :(得分:10)
你不应该使用pathMatch: 'full'
export const Product1Routes: Routes = [
{
path: '',
component: Product1Component,
children:[
{
path: 'dashboard',
loadChildren: 'app/product1/dashboard/dashboard.module#DashboardModule'
// or './dashboard/...
},
{
path: '',
component: Product1ViewComponent
}
]
}
];
我也改变了loadChildren
路径。 (已添加app/product1
)
为什么要为每个延迟加载的模块导入HttpModule
?
这在延迟加载的模块中也是多余的
providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }],
P.S。我会为每个模块创建Routing modules
app-routing.module.ts
product1-routing.module.ts
dashboard-routing.module.ts
等等
答案 1 :(得分:0)
可能与此问题相关(已在Angular 5.2.1上修复):Angular router '**' wildcard as a catch-all with child routes? Using latest 2.4.0 and Router 3.4.1
这是路由器的一个错误。
答案 2 :(得分:0)
这是一个使用嵌套路由进行延迟加载的工作示例
https://github.com/PrashantMaheshwari-SoftwareEngineer/nested-route-lazy-loading
export const mainRoute: Route[] = [
{ path: "login", component: LoginComponent },
{ path: "home", loadChildren: "./layout/layout.module#LayoutModule" },
{ path: "", redirectTo: "/login", pathMatch: "full" }
];
export const layoutRoute: Route[] = [
{
path: "",
component: LayoutComponent,
children: [
{
path: "dashboard",
component: DashboardComponent
},
{
path: "employee",
component: EmployeeComponent
},
{
path: "customer",
component: CustomerComponent
},
{
path: "",
redirectTo: "dashboard",
pathMatch: "full"
}
]
}
];