在App.Component中获取当前路线不起作用

时间:2018-10-22 15:38:44

标签: angular angular-routing

在我的有角项目中,我为台式机和移动版安装了不同的延迟加载模块和组件。

当我访问我的页面时,我不希望该应用程序确定它是移动设备还是更大的(台式机)设备。根据我路由到/mobile/.../desktop/...的情况。

我从以下代码开始:

if (window.innerWidth < 450) {
  this.router.navigateByUrl('/mobile');
} else {
  this.router.navigateByUrl('/desktop');
}

到目前为止,该方法仍然有效,但是存在一个问题。当我在/mobile/page1之类的路线上并重新加载页面时,我回到/mobile

为防止这种情况,我尝试检查当前路由,并查看其路由是/mobile还是/desktop,然后正确路由。像这样:

const currentRoute = this.router.url;
const isOnMobile = currentRoute.split('/')[1] === 'mobile' ? true : false;

if (window.innerWidth < 450) {
  if (!isOnMobile) {
    this.router.navigateByUrl('/mobile');
  }
} else {
  if (isOnMobile) {
    this.router.navigateByUrl('/desktop');
  }
}

但是我似乎无法在我的app.component中获得该url。该网址始终只是“ /”。

有什么想法吗?

0 个答案:

没有答案
相关问题