[英]Children of Lazy Loaded Modules routes don't work
I can't get the routing of a lazy loaded module working.我无法让延迟加载模块的路由正常工作。 Please have a look at this stackblitz , it's a copy from the Angular lazy loading example with a few changes.
请看一下这个stackblitz ,它是Angular 延迟加载示例的副本,有一些更改。
OrdersComponent
)OrdersComponent
)OrdersComponent2
)OrdersComponent2
)pathMatch: 'full'
in the child route configuration I'll get an error Error: Cannot match any routes. URL Segment: 'orders/child'
pathMatch: 'full'
时,我会收到错误Error: Cannot match any routes. URL Segment: 'orders/child'
Error: Cannot match any routes. URL Segment: 'orders/child'
for the child route Error: Cannot match any routes. URL Segment: 'orders/child'
What am I doing wrong here?我在这里做错了什么?
you should not use pathMath: full
when you have child-cmps.当你有 child-cmps 时,你不应该使用
pathMath: full
。
There are two possibilities有两种可能
<p>
orders works!
</p>
<router-outlet></router-outlet>
const routes: Routes = [
{
path: "",
children: [
{
path: "",
component: OrdersComponent
},
{
path: "child",
component: OrdersComponent2
}
]
}
];
EDIT:编辑:
or remove children and setup like this:或删除孩子并像这样设置:
const routes: Routes = [
{
path: "",
component: OrdersComponent
},
{
path: "child",
component: OrdersComponent2
}
];
let me know if this helps让我知道这是否有帮助
You defined OrdersComponent2
as a child route to OrdersComponent
: this has nothing to do with lazy loaded modules.您将
OrdersComponent
定义为OrdersComponent2
的子路由:这与延迟加载的模块无关。 They belong to the same module, that is lazy loaded once you navigate to /orders
.它们属于同一个模块,一旦导航到
/orders
就会延迟加载。
What you're doing by clicking the "Order Child" button is navigating to the child route /orders/child
: for that to work you need a <router-outlet>
in the orders.component.html template
.您通过单击“订购子”按钮正在执行的操作是导航到子路由
/orders/child
:为此,您需要在orders.component.html template
中使用<router-outlet>
。 That outlet will be used by Angular to display the child view. Angular 将使用该插座来显示子视图。
In your orders-routing-module.ts, the problem is where you specify the path:在您的 orders-routing-module.ts 中,问题在于您指定路径的位置:
path: "child",
There are no components with the selector "app-child", so it will give you the "cannot match any routes" error.没有带有选择器“app-child”的组件,因此它会给您“无法匹配任何路由”错误。 Your orders.component.ts has the child component with selector "app-orders-2", so try this path and the error should disappear and the content show:
您的 orders.component.ts 具有带有选择器“app-orders-2”的子组件,因此请尝试此路径,错误应该消失并且内容显示:
path: "orders-2",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.