wait function complete

我如何等待this.Auth.ReLoad();做完才繼續往下做?

import { Component, OnInit, ViewChild } from ‘@angular/core’;
import { ShowAlert } from “…/service/ShowAlert.service”;
import { AuthReLoad } from “…/service/AuthReLoad.service”;
import { Default } from “…/model/Default.model”;
import { Cookie } from ‘ng2-cookies/ng2-cookies’;
import { RouterLink } from “…/service/RouterLink.service”;
import { DataBaseLink } from “…/service/DataBaseLink.service”;

@Component({
selector: ‘app-news’,
templateUrl: ‘./news.component.html’,
styleUrls: [’./news.component.css’]
})

export class NewsComponent implements OnInit {

constructor(private Connection: DataBaseLink, private Show_Alert: ShowAlert, private Auth: AuthReLoad, private Default_fun: Default, private Route: RouterLink) { }

Auth_News: string;
LV: string;
Factory_spinner = [];
news_kind = [];
main_body = [];
page_num = [];
page_num_val: number = 30
Order: string = “desc”;
pageNum: number = 0;
totalRows: number;
Current_page_num: number = 30;
Page_option: number = 30;
type: string = “”;
kind: string = “”;
subject: string;
before: string;
after: string;
release_to: string = “”;
first: string = “”;
isVisable: string = “”;

ngOnInit() {
this.Route.Loading();
this.Auth.ReLoad();
localStorage.setItem(‘page_order’, this.Order);
localStorage.setItem(‘Current_page_num’, this.Page_option.toString());
this.Auth_News = sessionStorage.getItem(‘Auth_News’);
this.LV = Cookie.get(‘LV’);
this.Factory_spinner = this.Default_fun.news_type();
this.news_kind = this.Default_fun.news_kind();
this.page_num = this.Default_fun.Page();
this.Connection.Query_Post(“News_main/Index”, {
page_order: this.Order,
pageNum: this.pageNum,
Current_page_num: this.Page_option,
Auth_News: sessionStorage.getItem(‘Auth_News’)
}).subscribe(
data => {
this.main_body = data;
console.log(data);
if (data.length == this.Page_option) {
this.totalRows = Math.floor(data.length / this.Page_option) - 1;
}
else {
this.totalRows = Math.floor(data.length / this.Page_option);
}

    this.totalRows = Math.max(this.totalRows, this.pageNum);
    console.log(this.Auth_News);
  },
  error => {
    this.Show_Alert.Show_error();
  }
);
this.Route.Loading_cancel();

}

Auth.ReLoad最後才做完

1 在 Auth.Reload 方法回傳 promise,2或是Auth.Reload 回傳subscription,3或是Auth 加上 subject,然後訂閱它

1: Auth.Reload() {
return http.get(…).toPromise();
}

2:
Auth.Reload().subscribe(_ => …接下來要做的事

3:
Auth.Loaded$.subscribe(_ => …接下來要做的事

感謝給予方向,我透過.subscribe測試成功
不過@Aidan_Chang,.subscribe(_ => -->底線是幹嘛的啊?

既然你測試成功了
就能假設你應該能知道那個是放回傳的變數了

猜測應該是想知道為什麼命名"_“當變數名稱
我猜應該是只是要代表這個變數不會用到而已
而命名成”_"(從Golang的想法過來的)