📜  桌面和移动 Angular 的不同视图 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:24.090000             🧑  作者: Mango

代码示例1
// Route guards would be a good solution for this purpose.

// The MobileGuard would look like this :

export class MobileGuard implements CanActivate {
    constructor(private _router: Router, private _mobileService: MobileService) {}

    canActivate(): boolean {
        const isMobile = this._mobileService.isMobile();
        if(!isMobile) {
            this._router.navigate(['/desktop']);
        }
        return isMobile;
    }
}
// The same for the DesktopGuard.

// The user trying to access any of the routes, would be redirected to the right one.