简体   繁体   English

如果页面仍在加载,Angular router.navigateByUrl()无法正常工作

[英]Angular router.navigateByUrl() not working if page is still loading

I am trying to integrate auth0 into my angular app. 我正在尝试将auth0集成到我的角度应用程序中。

Auth0 login event flow happens like below. Auth0登录事件流程如下所示。

  1. Angular page shows the auth0 login widget (The code for showing the login widget is located inside a service: AuthService ). 角度页面显示auth0登录小部件(用于显示登录小部件的代码位于服务内部: AuthService )。

  2. User picks the authentication provider from the login widget (eg: login with google). 用户从登录小部件中选择身份验证提供程序(例如:使用google登录)。

  3. Browser gets redirected to the authentication provider. 浏览器被重定向到身份验证提供程序。

  4. User enters credentials in the authentication provider. 用户在身份验证提供程序中输入凭据。

  5. Browser gets redirected back to the angular site, to the configured callback URL. 浏览器将重定向回到有角度的站点,再配置的回调URL。 In my case, this is a page which shows a loading indicator (since login is still not complete). 就我而言,这是一个页面,其中显示了加载指示符(因为登录仍未完成)。

  6. A callback event gets fired from the auth0 login widget with the authentication token. 使用身份验证令牌从auth0登录小部件触发回调事件。

  7. Inside the callback, A REST call is made to get more information about the user. 在回调内部,进行REST调用以获取有关用户的更多信息。

  8. When the REST call finishes, router.navigateByUrl('/home') is called to return to home, after saving retrieved user information to local storage. REST调用完成后,在将检索到的用户信息保存到本地存储中之后,将调用router.navigateByUrl('/home')以返回主目录。

But, when I call router.navigateByUrl('/home') , the navigation doesn't happen. 但是,当我调用router.navigateByUrl('/home') ,导航不会发生。 But, if I call router.navigateByUrl('/home') after about a 5 second timeout, the navigation happens successfully. 但是,如果我在大约5秒钟的超时后调用router.navigateByUrl('/home') ,则导航成功完成。 I suspect that for the navigation to happen, browser loading should have completed when the navigateByUrl() call is made. 我怀疑要进行导航,应该在进行navigationByUrl navigateByUrl()调用时完成浏览器加载。

Does anyone know a way to do the navigation reliably, without using a timeout? 有谁知道可靠地进行导航而不用超时的方法?

Following is the AuthService code. 以下是AuthService代码。

@Injectable()
export class AuthService {
    lock = new Auth0Lock(
        'my_client_id',
        'my_auth0_sub_domain',
        {
            auth: {
                redirectUrl: window.location.origin + '/auth_loading',
                responseType: 'token',
                scope: 'openid'
            }
        }
    );

    userProfile: Object;

    constructor(public router: Router) {
        this.userProfile = JSON.parse(localStorage.getItem('profile'));
        this.lock.on("authenticated", (authResult: any) => {
            localStorage.setItem('id_token', authResult.idToken);
            this.lock.getProfile(authResult.idToken, (error: any, profile: any) => {
                if (error) {
                    alert(error);
                    return;
                }
                localStorage.setItem('profile', JSON.stringify(profile));
                this.userProfile = profile;
                setTimeout(() => {
                    this.router.navigateByUrl('/home'); //This only works because of the timeout
                }, 5000);
            });
        });
    }

    public login() {
        this.lock.show();       //This shows the auth0 login widget
    }
}

我将auth0-lock从v10升级到v11,现在可以正常使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular中router.navigateByUrl和router.navigate的使用方法 - How to use router.navigateByUrl and router.navigate in Angular router.navigateByUrl()导致导航到根并重新加载,角度为2.4.7 - router.navigateByUrl() causes navigation to root and reload, angular 2.4.7 router.navigateByUrl不发送值Angular - router.navigateByUrl don't send value Angular 如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then - How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine routerLinkActive 不适用于 router.navigateByUrl 或导航 - routerLinkActive does not work with router.navigateByUrl or navigate 嘲笑 Angular navigateByUrl 仍在运行页面重新加载 - Mocked Angular navigateByUrl still running page reload 将router.navigateByUrl之后的组件刷新为具有不同参数的相同URL - Refresh component after router.navigateByUrl to the same URL with different parameters 如何在使用 Router.navigateByUrl 时将数据传递给组件 - How to pass data to a component while using Router.navigateByUrl Angular Router:用false解析的navigationByUrl() - Angular Router: navigateByUrl() resolving with false CanDeactive Guard:通过 router.navigateByUrl() 路由时如何重置活动 class - CanDeactive Guard: How to reset active class when routing by router.navigateByUrl()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM