简体   繁体   English

angular 6函数未按顺序执行所有代码行

[英]angular 6 function not execution all the lines of code in order

I am learning to Angular 6, and am trying to use Material Modals as a selector. 我正在学习Angular 6,并尝试使用Material Modals作为选择器。

The modal returns an object with 3 mongoose id's : {userID: '', partyID: '', teamID:''} 模态返回一个带有3个猫鼬ID的对象:{userID:``,partyID:'',teamID:''}

These then set stored in localstorage all successfully. 然后将这些设置成功存储在本地存储中。

my problem is :- 我的问题是:

I want it so if the page loads initially and there is tokens in localstorage, it goes to either a welcome page, the song page or the admin menu. 我想要这样,如果页面最初加载且localstorage中有令牌,它将转到欢迎页面,歌曲页面或管理菜单。

BUT : it does not evaluate all the partyValues etc before it gets to the router.navigate section. 但是:在进入router.navigate部分之前,它不会评估所有partyValues等。

* my code * *我的代码*

ngOnInit() {
    console.log('INTO Init ', this.userLoggedIn, this.teamLoggedIn, this.partyLoggedIn);
    this.evaluateTokens();
    console.log('OUT Init ', this.userLoggedIn, this.teamLoggedIn, this.partyLoggedIn);

  }

  evaluateTokens(){
    let user  = localStorage.getItem('user');
    let party = localStorage.getItem('party');
    let team  = localStorage.getItem('team');
    if(!!user){
      this._User.details(user).subscribe(
        data => { console.log('setting user values ');
          this.userValues = data;
          this.userLoggedIn = true;
          this.teamLoggedIn = false;
          this.partyLoggedIn = false;
        },
        err => console.log('User details error : ',err)
      );
    } else {
      if(!!team){
        this._Team.details(team).subscribe(
          data => { console.log('setting team values ');
            this.teamValues = data;
            this.teamLoggedIn = true;},
          err => console.log('Team details error : ',err)
        );
      }
      if (!!party){
        this._Party.details(party).subscribe(
          data => { console.log('setting party values '); 
            this.partyValues = data;
            this.partyLoggedIn = true;},
          err => console.log('Party details error : ',err)
        );
      }
    }
    console.log('After If Statements : ', this.userLoggedIn, this.partyLoggedIn, this.teamLoggedIn);
    if(this.userLoggedIn){
      console.log('go to admin');     
      this._router.navigate(['/admin']);
    } else if(this.partyLoggedIn || this.teamLoggedIn){
      console.log('go to song search');
      this._router.navigate(['/songSearch']);
    } else {
      console.log('No Token');
      this._router.navigate(['/'])
    }
  }

the console output is :- 控制台输出为:

INTO Init  false false false  navbar.component.ts:74
After If Statements :  false false false navbar.component.ts:82
No Token navbar.component.ts:38
OUT Init  false false false core.js:3121
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
navbar.component.ts:67 setting party values

I think it is not waiting for the services to respond before it continues to the next section. 我认为在继续下一节之前,它不会等待服务响应。

Any help would be most welcome. 任何帮助将是最欢迎的。

Dave 戴夫

Try doing like this. 尝试这样做。 Add the desired navigation inside the subscribe . 添加里面的所需导航subscribe

For an instance, 例如

this._User.details(user).subscribe(
    data => { 
      this.userValues = data;
      this.userLoggedIn = true;
      this.teamLoggedIn = false;
      this.partyLoggedIn = false;
      this._router.navigate(['/admin']);
    },
    err => console.log('User details error : ',err)
  );

You clearly got it right by understanding the flow, saying that navigation doesn't wait until the response gets back from the server. 通过了解流程,您显然可以正确地做到这一点,并说导航不会等到响应从服务器返回后再进行。

All you need to do is perform the navigation inside the subscribe so it is guaranteed that navigation happens after the response. 您需要做的就是在subscribe执行导航,以确保导航在响应之后发生。

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM