首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将来自不同延迟加载模块的两个组件绑定到同一路由?

如何将来自不同延迟加载模块的两个组件绑定到同一路由?
EN

Stack Overflow用户
提问于 2018-04-13 17:57:37
回答 2查看 1.7K关注 0票数 4

我有一个Angular应用程序,其结构如图所示:

要根据从服务器检索到的数据有条件地选择其中一个主题。

代码语言:javascript
复制
const routes: Routes = [
  {
    path: '',
    loadChildren: 'app/presentation/theme1/theme1.module#Theme1Module',
    canActivate: [Theme1Guard],
  },
  {
    path: '',
    loadChildren: 'app/presentation/theme2/theme2.module#Theme2Module',
    canActivate: [Theme2Guard],
  }
];

theme-1theme-2模块都具有到不同布局和样式的相似组件的相同路由。

更新1

我尝试了CanActivate卫士,一个用于theme-1,另一个用于theme-2。每个守卫从themeStore中检索当前主题名称,并将其与当前路由进行比较:

代码语言:javascript
复制
canActivate() {
    let currentTheme: string = '';
    this.themeStore.currentTheme.subscribe((themeName) => {
      currentTheme = themeName;
    });

    if (currentTheme == 'theme1') {
      return true;
    }
    else {
      return false;
    }
  }

然而,这不会起作用,因为在第一个路径被CanActivate guard拒绝后,Angular路由器不会查找相同的路径。

更新2

在Angular存储库中有一个开放的问题- Load a component in a route depending on an asynchronous condition。这似乎是几个月前添加到积压中的。

EN

回答 2

Stack Overflow用户

发布于 2018-04-13 19:12:49

主题-1和主题-2都有相同的路径到不同布局和样式的相似组件。

无延迟加载

创建theme-1theme-2路由:

代码语言:javascript
复制
{
    path: 'theme-1', component: Theme1Component,
    children: [
      {
        path: 'page', 
        component: PageComponent,
      }

    ]
},
{
    path: 'theme-2', component: Theme2Component,
    children: [
      {
        path: 'page', 
        component: PageComponent,
      }
    ]
},

使用延迟加载

如果它们是可延迟加载,则在主路由模块

代码语言:javascript
复制
const routes: Routes = [
  {
    path: '',     
    children: [
      {
        path: 'theme-1',         
        loadChildren: 'path/to/theme1#module',
      },
      {
        path: 'theme-2',         
        loadChildren: 'path/to/theme2#module',
      }

    ]
  },
  ...
];

延迟加载theme-1theme-2模块路由:

theme1-routing.module:

代码语言:javascript
复制
const routes: Routes = [
    {
        path: '',
        component: Theme1Component,

        children: [
            {
              path: 'page', 
              component: PageComponent,
            },           
        ]
    }
];

theme2-routing.module:

代码语言:javascript
复制
const routes: Routes = [
    {
        path: '',
        component: Theme2Component,

        children: [
            {
              path: 'page', 
              component: PageComponent,
            },           
        ]
    }
];
票数 2
EN

Stack Overflow用户

发布于 2018-04-13 19:10:34

我认为您的第二个模块的路由已被覆盖,请尝试将您的路由放在第二个模块的导入的第一个位置:

代码语言:javascript
复制
 // in your second module
 imports: [
    routing, 
    ... // then import the others 
],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49814235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档