为什么在ngOnInit中无法访问angular2对象?

布山·加德卡(Bhushan Gadekar)

我现在花了相当长的时间来研究angular2,想知道为什么我不能访问其中的对象,ngOnInit()但是可以在服务调用中访问它们。对于前。

import { Component} from 'angular2/core';
import { GlobalService} from './../../app/shared/services/global/global.service';
import { GlobalObjectsService} from './../../app/shared/services/global/global.objects.service';
import { WorkspaceService } from './../../app/shared/services/pm/workspaces.service';
import { ROUTER_DIRECTIVES, RouteConfig } from 'angular2/router';
import {ProjectsService} from './../../app/shared/services/pm/projects.service';
import {PagesService} from './../../app/shared/services/pm/pages.service';
import { WorkspacesComponent} from './../../app/project-manager/workspaces/workspaces.component';
import { ProjectsComponent } from './../../app/project-manager/projects/projects.component';
import { PagesComponent } from './../../app/project-manager/pages/pages.component';

@Component({
    selector: 'project-manager',
    templateUrl: "app/project-manager/project-manager.component.html", 
    providers: [WorkspaceService,ProjectsService,PagesService],
    directives: [ROUTER_DIRECTIVES,WorkspacesComponent,ProjectsComponent,PagesComponent]
})
export class ProjectManegerComponent { 
    public workspaces: any;
    public projects: any;
    public pages: any;  
    baseUrl: string;

    constructor(private globalService: GlobalService, private globalObjectsService: GlobalObjectsService,private workspaceService: WorkspaceService,private projectsService:ProjectsService,private pagesService:PagesService) {
        this.baseUrl = this.globalService.getBaseUrl();        
        this.workspaceService.getAllWorkspaces(this.baseUrl)
        .subscribe((workspaces) => {                
              this.workspaces=workspaces;
              this.globalObjectsService.selectedWorkspace=workspaces[0]; 
              console.log(this.globalObjectsService.selectedWorkspace);           
              //able to access here                    
            }
        );        
        this.projectsService.getAllprojects(this.baseUrl)
        .subscribe((projects) => {
            this.projects = projects;            
            }
        );
        this.pagesService.getAllpages(this.baseUrl)
        .subscribe((pages) => {
            this.pages = pages;         
            }
        );
    }

    ngOnInit() { 
        console.log(this.globalObjectsService.selectedWorkspace);
        //cannot access here
    }   
}

因此,我很想知道如何才能在ngOnInit中使它可访问?

蒂埃里圣堂武士

这是因为this.globalObjectsService.selectedWorkspace是在订阅回调中异步设置的。在执行ngOnInithook方法之前,组件无需等待其构造函数中触发的异步处理完成

看:

this.workspaceService.getAllWorkspaces(this.baseUrl)
    .subscribe((workspaces) => {                
      this.workspaces=workspaces;
      this.globalObjectsService.selectedWorkspace=workspaces[0]; //<------
    });

ngOnInit方法“仅”发生在组件的生命周期的一部分。

编辑

如果要基于该selectedWorkspace属性触发处理,则可以在订阅回调中执行它们:

constructor() {
  this.workspaceService.getAllWorkspaces(this.baseUrl)
    .subscribe((workspaces) => {                
      this.workspaces=workspaces;
      this.globalObjectsService.selectedWorkspace=workspaces[0];
      this.executeSomething(); //<------
    });
}

executeSomething() {
  console.log(this.globalObjectsService.selectedWorkspace); //<------
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法访问 angular2 中的 dom 元素

无法访问Angular中的对象属性

为什么会出现错误:使用EF和AutoFac的.net core 2中的“无法访问已处置的对象”?

为什么我无法访问在对象的方法中创建的对象

为什么sx道具无法访问主题对象?

为什么我无法访问数组内部的对象值?

为什么类对象无法访问类中定义的委托

为什么函数无法访问外部声明的对象中的局部变量

为什么我无法访问 JSON 对象字典中的变量?

Angular2 FullCalendar回调无法访问的属性

Angular2自定义表单验证器无法访问类中的“ this”

setInterval()无法访问Angular2中的指令函数的问题

为什么onCreateView中的WebView代码无法访问?

为什么此代码警告在Groovy中无法访问?

为什么我在dbset 中无法访问update 方法?

为什么无法访问的代码在C ++中不是错误?

为什么在Rails模型中无法访问会话

为什么我无法访问JSON中的数据?

为什么无法访问模块中的此公共功能

为什么在单元测试中无法访问该类?

为什么在角组件中无法访问范围变量

为什么在 navigator.geolocation.getCurrentPosition() 为对象赋值并返回后,我无法访问对象中的值?

在Angular中返回对象后无法访问代码

Angular2无法在promise中访问“ this”

为什么该语句无法访问?

为什么分区无法访问?

为什么WebWorkers无法访问DOM?

为什么无法访问 DataFrame?

为什么我无法访问 RunCommandLineApplicationAsync?