简体   繁体   English

如何使Angular组件UI等待异步功能

[英]How to make Angular component UI wait for Asynchronous function

What I am doing is I am subscribing a http get function that validates my user in my singup component. 我正在做的是,我正在预订一个http get函数,该函数可以在singup组件中验证我的用户。 Now what I want is I want to make it wait until I get the data and navigate away to my internal page. 现在,我想要的是等待直到获取数据并导航到内部页面。 I am trying to navigate to internal page after validation is successful. 验证成功后,我试图导航到内部页面。 But it works just after UI is ready. 但它仅在UI准备好后才起作用。 I mean I still see signup page after refreshing for 1 second. 我的意思是刷新1秒钟后仍然看到注册页面。 The code is given below 代码如下

  this._restapiService.validate()
          .subscribe(data=>{
              if(data.success){
                this._router.navigate(['contacts']);
              }
          });

I tried to put this code in constructor() and ngInit() but same thing is happening. 我试图将这段代码放在Constructor()和ngInit()中,但是正在发生同样的事情。

As mentioned by @yurzui in the comments section, angular guards prevent views from rendering if the guard validation fails (component life cycle will not be triggered if guard fails). 如@yurzui在评论部分中所述,如果防护验证失败(如果防护失败,则组件生命周期将不会触发),角度防护会阻止呈现视图。

Checkout this sample code snippet that you can use to add a guard for authenticated views in your application - 检出此示例代码片段,可用于在应用程序中为经过验证的视图添加保护措施-

Guard Definition 守卫定义

import { Injectable } from "@angular/core";
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";

@Injectable()
export class LoggedInGuard implements CanActivate {

    constructor() { }

    public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {
        return new Promise<any>((resolve: Function, reject: Function) => {
            //this is where you need to validate the user
            //it can be an AJAX call
            let response: any;
            //assuming the AJAX call is made here
            //response = HttpService.getData();

            //resolve indicates user is validated by the service and guard allows user to land on the reuqested view.
            //reject on the other hand, will stop user from landing on requested view
            //this logic can be customised.
            response.success ? resolve() : reject();
        });
    }
}

Route Definition 路线定义

import { Route } from "@angular/router";
import { HomeComponent } from "./components/home.component";
import { LoginComponent } from "./components/login.component";

export const routes: Route[] = [{
        path: "route",
        canActivate: [LoggedInGuard],
        component: HomeComponent,
    },{
        path: "*",
        component: LoginComponent,
    }];

Do checkout this SO answer to know how to trigger multiple guards in series as it most of the times creates problems because angular doesn't fire guards in series. 请检查这个SO答案,以了解如何串联触发多个警卫,因为在大多数情况下,角度触发不会串联触发多个警卫。

I hope this helps you. 我希望这可以帮助你。

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

相关问题 如何使JS function 等待组件加载到 angular - How to make a JS function wait for the component to be loaded in angular 如何在角度使异步功能? - How to make asynchronous function in angular? 如何使函数等待异步函数(http post 请求)在 angular 2 中完成? - How to make a function to wait until asynchronous function (http post request) to complete in angular 2? 如何让 Angular2 在渲染组件之前等待承诺 - How to make Angular2 wait for a promise before rendering component Angular 2 如何让子组件等待异步数据准备就绪 - Angular 2 how to make child component wait for async data to be ready 如何使一个函数在angular2中等待另一个函数完成执行 - How to make a function wait for another function to finish excecution in angular2 angular:等待异步数据 - angular: wait for asynchronous data 如何使函数与组件角度分开 - how to make a function separate from component angular Angular - 如何使用不在组件上的 function 在点击时制作按钮? - Angular - How to make a button on click with a function that is not on the component? 如何在Angular 5中使组件侦听并等待来自另一个组件的数据而没有事件? - How to make a component listen and wait for data from another component without an event in Angular 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM