简体   繁体   English

Angular 子路由在直接加载时重定向,在那里导航工作正常

[英]Angular child route redirects when loaded directly, navigating there works fine

My Angular child route can be navigated to but cannot be refreshed or loaded directly.我的 Angular 子路由可以导航到但不能直接刷新或加载。

const routes: Routes = [
    {
        path: '',
        component: HomeComponent,
        data: { num: 1 }
    },
    {
        path: 'page', component: PageComponent, data: { num: 2 },
        children: [
            { path: ':id', component: PageComponent } 
        ]
    }
];

linking to http://localhost:4300/page/715497 loads the page and I have access to the param 715497 .链接到 http://localhost:4300/page/715497 加载页面,我可以访问参数715497 Then reloading the page redirects back home.然后重新加载页面重定向回家。 Why would this be happening?为什么会发生这种情况?

In order for this route to be rendered, a <router-outlet> must be available in the RootComponent .为了渲染这条路线, <router-outlet>必须在RootComponent可用。 router-outlet> is an Angular directive from the router library that is used to insert the component matched by routes to be displayed on the screen. router-outlet>是路由器库中的一个 Angular 指令,用于插入要显示在屏幕上的路由匹配的组件。 The <router-outlet> isn't linked to the component, it is linked to the child routes of the component <router-outlet>不链接到组件,它链接到组件的子路由

RouterOutlet documentation RouterOutlet 文档

A good, understandable example with Angular 11 Routing Angular 11 Routing 的一个很好的、易于理解的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM