简体   繁体   English

延迟加载模块路由的子节点不起作用

[英]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 延迟加载示例的副本,有一些更改。

  • First of all, the lazy loading itself seems to be working correctly首先,延迟加载本身似乎工作正常
  • If I click the "orders" button the module is (lazy) loaded and the correct component is being displayed ( OrdersComponent )如果我单击“订单”按钮,则模块已(延迟)加载并且正在显示正确的组件( OrdersComponent
  • However, clicking "Orders Child" also displays the orders component (instead of the OrdersComponent2 )但是,单击“Orders Child”也会显示订单组件(而不是OrdersComponent2
  • When I un-comment the 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有两种可能

  1. add a router-outlet to your orders component if your orders2 should be nested within the orders component.如果您的订单 2 应该嵌套在订单组件中,请向您的订单组件添加一个路由器出口。
<p>
  orders works!
</p>

<router-outlet></router-outlet>
  1. move the orders component to children:将订单组件移动到子组件:
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 将使用该插座来显示子视图。

Updated Stackblitz更新了 Stackblitz

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.

相关问题 延迟加载模块中的角子级路由无法完全正常工作 - Angular children routes in Lazy Loaded module don't completely work Angular 7 嵌套延迟加载子路由 - Angular 7 Nested Lazy Loaded Children Routes Angular 路由器:命名的出口似乎不适用于相对路由,也不适用于延迟加载的模块 - Angular router: named outlets does not seem to work with relative routes nor in lazy loaded modules 延迟加载模块的路由完全重新加载应用程序 - routes of lazy loaded modules completely re-loads the app 列出 Angular 9 应用懒加载模块的所有路由 - List all routes of Angular 9 app with lazy loaded modules 延迟加载模块中的 Angular single-spa 延迟加载路由调用未定义的 webpack 错误 - Angular single-spa lazy loaded routes in lazy loaded modules give call of undefined webpack error Angular 4-延迟加载路线不起作用 - Angular 4 - Lazy Loading routes doesn't work 子路由在延迟加载中不起作用 - child routes doesn't work in lazy loading 延迟加载的 Angular 模块无法使用@nguniversal 渲染服务器端,而客户端路由和渲染工作 - Lazy-loaded Angular modules don't get server side rendered with @nguniversal, while client side routing and rendering works 在延迟加载的模块中引导 - Bootstrapping in lazy loaded Modules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM