【发布时间】:2017-11-29 10:48:30
【问题描述】:
在我正在开发的应用程序中,它需要桌面和移动完全两种不同的视图。不可能使用 CSS 作为内容完全不同。
我做了什么?
我在 Routing file 中使用代码 const isMobile = window.innerWidth < 768;
在路径组件中:component: isMobile ? MobileComponent : DesktopComponent
这在开发环境中运行良好。但是,虽然生产构建失败了ng serve --prod(显然是 Angular 5)
问题:
完全没有错误,根据isMobile true/false 它应该加载MobileComponent/DesktopComponent。但即使 isMobile 为真/假也不会加载
始终在桌面和移动设备中加载 MobileComponent。虽然isMobile 值计算正确,true 用于Mobile,false 用于Desktop
我认为原因是路由在发送到客户端之前已经编译。
尝试了解决方法但不起作用:
使用 if..else 条件,同时计算 isMobile 并在路由文件中给出该变量(组件)而不是三元运算
任何方式:
有什么方法可以实现相同的功能吗?
提前致谢
【问题讨论】:
-
AOT 中的错误是什么?
-
@Dhyey 完全没有错误,即使 isMobile 为真/假,也只是加载
MobileComponent -
你试过“window.screen.width”
-
@shanths isMobile 计算正确,三元运算无法正常工作
component: isMobile ? MobileComponent : DesktopComponent总是返回MobileComponent -
但对我来说它在生产中工作demo link
标签: javascript angular mobile