【问题标题】:Reloading child component route reloads the parent parent component重载子组件路由重载父父组件
【发布时间】:2021-05-31 07:26:39
【问题描述】:

我有一条父路线,在这条路线下我的子路线如下。

const routes : Routes = [
            {path: 'home', component: HomeComponent, 
            children: [
            { path: 'airetail', component: AiretailplatformComponent},
            { path: 'cpr', component: CprComponent },
            { path: 'snap', component: SnapComponent},
            { path: 'others', component: OthersComponent },
            { path: 'docs', component: DocumentationComponent },
            {path: 'dynamic', component: AiRetailDynamicComponent}
            ],
            {path: '', redirectTo: '/home', pathMatch: full}
        }
     ]

每当我在任何子路由中并尝试重新加载任何子路由组件时,父路由也会被调用。有没有一种方法可以限制调用我的父路由,这里是 HomeComponent 每当我重新加载子路由页面时?

【问题讨论】:

    标签: angular


    【解决方案1】:

    使用相对路径。

    在您的 .component.ts 中

    constructor(
    private activateRoute: ActivatedRoute, 
    private router: Router)  {
    
       routeToPath() {
          this.router.navigate(['<your-path>'], { relativeTo: activateRoute 
          })
       }
    }
    

    这只会加载子路由。

    【讨论】:

    • 我在我的解决方案中使用 routerLink 导航到工作正常的子路由。但我只在单击浏览器刷新按钮时遇到问题,并且主组件模板与我的子组件一起重新加载。
    • 那你期待什么?
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2019-05-06
    • 2019-12-02
    • 2018-10-31
    • 2018-03-09
    • 2016-09-04
    • 2016-09-05
    • 2018-03-24
    相关资源
    最近更新 更多