Angular -> 向其他 Class 输出价值并在网站上显示价值

Angular -> Exporting Value to other Class and Displaying Value on the Website

我想从 Class 的 CookieService 中导出一个值并将其导入另一个 class,然后我想在我的网站上显示导入的值。 下面是我的代码。

我试过的 Cookie 服务和导出:

public login(code: string, state: string) {
    const paramsString = `?code=${code}&state=${state}`;
    this.oAuthService.getToken(paramsString).subscribe((response) => {
      const dateNow = new Date();
      dateNow.setSeconds(dateNow.getSeconds() + 720);
      this.cookieService.set("access_token", response.access_token, {expires: dateNow});
      this.cookieService.set("user_info", response.user_info.user_name, {expires: dateNow});
      this.cookieService.set("user_email", response.user_info.email, {expires: dateNow});
      this.cookieService.set("user_id", response.user_info.user_id, {expires: dateNow});
    });
  }
  public setUserName() {
    return this.cookieService.get("user_info");
  }

我尝试通过这样做导入它,导入来自 Class AppComponent:

readonly random?: AppComponent

然后我尝试在网站上显示导出值,但它不起作用:

<div class="row">
      <div class="col-sm margin-top-3">
        Order Permissions for {{random.setUserName().toString()}}
        <br>
        <button
          style="width: 10%"
          id="orderSinglePermission"
          class="button button--primary margin-top-3"
        > Order
        </button>
      </div>

我认为这样在插值中使用函数是不可能的,所以你可以在TS文件中这样做并在HTML中使用值。

例如:

ts 文件:

constructor(your service name){}

get userName(){
    return this.serviceName.setUserName();
}

html:

{{userName().toString()}}