[英]Dynamic redirect angular routes to load dynamic modules
I have a big problem.我有一个大问题。 I try many solutions without results :( I want to load a module depending from screen size to view mobile view or web view.我尝试了许多没有结果的解决方案:( 我想根据屏幕大小加载一个模块以查看移动视图或 Web 视图。
The most near to reach my objetive is that code, but it have many errors.最接近我的目标的是那个代码,但它有很多错误。
When the screen is <768
and I set http://localhost:4200/
load web but mobile too.当屏幕为<768
并且我设置http://localhost:4200/
加载 web 但也是移动的。 =( =(
When the screen is >768
and I set http://localhost:4200/
load web.当屏幕>768
并且我设置http://localhost:4200/
加载 web。 =) =)
When the screen is <768
and I set http://localhost:4200/mobile/etc/etc/etc
load mob and web and redirect to http://localhost:4200/mobile
.当屏幕为<768
并且我设置http://localhost:4200/mobile/etc/etc/etc
加载 mob 和 web 并重定向到http://localhost:4200/mobile
。 =( =(
When the screen is >768
and I set http://localhost:4200/web/etc/etc/etc
load web.当屏幕>768
并且我设置http://localhost:4200/web/etc/etc/etc
加载 web.conf 时。 =) =)
app-routing.module.ts app-routing.module.ts
const routes: Routes = [
{
path: 'mobile',
loadChildren: './view/mobile/mobile.module#MobileModule',
canActivate: [MobGuardService],
data: {
preload: false
}
},
{
path: 'web',
loadChildren: './view/web/web.module#WebModule',
canActivate: [WebGuardService],
data: {
preload: false
}
},
{
path: 'error',
loadChildren: './view/error/error.module#ErrorModule',
data: {
preload: false
}
},
{
path: '',
redirectTo: window.innerWidth < 768 ? `/mobile/etc/etc/etc` : `/web/etc/etc/etc`,
pathMatch: 'full'
},
{
path: '**',
redirectTo: window.innerWidth < 768 ? `/mobile/etc/etc/etc` : `/web/etc/etc/etc`
}
];
mob-guard.service.ts mob-guard.service.ts
@Injectable({
providedIn: 'root'
})
export class MobGuardService implements CanActivate {
constructor(
private router: Router
) {
}
canActivate() {
if (window.innerWidth >= 768) {
this.router.navigate(['/']).then();
return false;
}
return true;
}
}
web-guard.service.ts web-guard.service.ts
@Injectable({
providedIn: 'root'
})
export class WebGuardService implements CanActivate {
constructor(
private router: Router
) {
}
canActivate() {
if (window.innerWidth < 768) {
this.router.navigate(['/m/']).then();
return false;
}
return true;
}
}
My objetive is simple but I dont know how do that:我的目标很简单,但我不知道该怎么做:
I try to to implement something like this, but dosent works:我试图实现这样的东西,但 dosent 工作:
{
path: window.innerWidth < 768 ? 'm' : 'w',
loadChildren: window.innerWidth < 768 ? './view/mob/mob.module#MobModule' : './view/web/web.module#WebModule',
canActivate: window.innerWidth < 768 ? [MobGuardService] : [WebGuardService],
data: {
preload: false
}
}
I hope help.我希望有所帮助。 Thanks for yor time.谢谢你的时间。
You can make a one root component where you can add a check for checking window inner width and you can redirect accordingly your window view or mobile view.您可以制作一个根组件,您可以在其中添加检查窗口内部宽度的检查,并且您可以相应地重定向您的窗口视图或移动视图。
@component({
selector:'app-home',
template:'',
})
constructor(private rout:router){
if(window.innerWidth>768){
this.rout.navigate(['/window'])
}else{
this.rout.navigate(['/mobile'])
}
}
I guess this could work for angular 9+我想这适用于 angular 9+
{
path: '',
loadChildren: () => window.innerWidth < 768 ? import(`/mobile/etc/etc/etc`).then(m => m.MobileModule) : import(`/web/etc/etc/etc`).then(m => m.WebModule),
pathMatch: 'full'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.