ng-template中未定义的全局变量

时间:2020-11-09 15:38:35

标签: javascript angular

我正在使用全局文件来设置应用程序所需的关键数据。一切正常,直到您使用浏览器刷新按钮。单击后,加载模板时,我从globals.ts文件加载的所有数据将以未定义的形式返回。模板加载后,promise将填充数据。我需要在ng-template绑定之前填充Promise数据。

请注意,我正在尝试通过从app.component调用this.globals.init(token)填充全局数据。然后,在有用的链接模板中,我尝试基于从父组件(myAccount.component)传入的数据构建一个url。奇怪的是,父组件中的数据也使用了globals文件,并且填充良好。我哪里出错了?

-app.component 构造函数(public globals:Globals){}

{
    "DockerId": "cd189a933e5849daa93386466019ab50-2495160603",
    "Name": "curl",
    "DockerName": "curl",
    "Image": "111122223333.dkr.ecr.us-west-2.amazonaws.com/curltest:latest",
    "ImageID": "sha256:25f3695bedfb454a50f12d127839a68ad3caf91e451c1da073db34c542c4d2cb",
    "Labels": {
        "com.amazonaws.ecs.cluster": "arn:aws:ecs:us-west-2:111122223333:cluster/default",
        "com.amazonaws.ecs.container-name": "curl",
        "com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:111122223333:task/default/cd189a933e5849daa93386466019ab50",
        "com.amazonaws.ecs.task-definition-family": "curltest",
        "com.amazonaws.ecs.task-definition-version": "2"
    },
    "DesiredStatus": "RUNNING",
    "KnownStatus": "RUNNING",
    "Limits": {
        "CPU": 10,
        "Memory": 128
    },
    "CreatedAt": "2020-10-08T20:09:11.44527186Z",
    "StartedAt": "2020-10-08T20:09:11.44527186Z",
    "Type": "NORMAL",
    "Networks": [
        {
            "NetworkMode": "awsvpc",
            "IPv4Addresses": [
                "192.0.2.3"
            ],
            "AttachmentIndex": 0,
            "MACAddress": "0a:de:f6:10:51:e5",
            "IPv4SubnetCIDRBlock": "192.0.2.0/24",
            "DomainNameServers": [
                "192.0.2.2"
            ],
            "DomainNameSearchList": [
                "us-west-2.compute.internal"
            ],
            "PrivateDNSName": "ip-10-0-0-222.us-west-2.compute.internal",
            "SubnetGatewayIpv4Address": "192.0.2.0/24"
        }
    ],
    "ContainerARN": "arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1",
    "LogOptions": {
        "awslogs-create-group": "true",
        "awslogs-group": "/ecs/containerlogs",
        "awslogs-region": "us-west-2",
        "awslogs-stream": "ecs/curl/cd189a933e5849daa93386466019ab50"
    },
    "LogDriver": "awslogs"
}

-Globals.ts

async ngOnInit() {
    ...
    this.globals.init(token);
    ...
}

-my7Account.component.html文件

    @Injectable()
    export class Globals {
    
      async init(token:string): Promise<any>{
  
    if(token === undefined || token === '') {
      token =  this.oidcSecurityService.getIdToken();
    }
   const decoded = jwt_decode(token);
    console.log(decoded, '<<<<<<<<<---------  decoded!', this.title)
   this.email = decoded.email;

   await this.get('getuserdata', {}).toPromise()
   .then(async (res) => {
       // THIS IS THE NEEDED DATA THAT COMES BACK AFTER THE BINDING.  all except localstorage is undefined
     localStorage.setItem('email', res['body'][0].email);
     this.userRole = res['body'][0].title;
     this.setUserId(res['body'][0].userid);
     this.userid = res['body'][0].userid;
     this.email = res['body'][0].email;
     this.subscriberid = res['body'][0].fk_tk_subscriber_id;
     this.isManagerRole = res['body'][0].title === 'HR Admin' ? true : false;
     this.setTitle(res['body'][0].title);
     this.profilePic = res['body'][0].profilephotourl;
   });
   }

-帮助链接模板

...
<app-tk-helpful-links [glob]='this.globals'></app-tk-helpful-links>
...

0 个答案:

没有答案
相关问题