简体   繁体   English

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

[英]why angular2 object is not accessible in ngOnInit?

I am working on angular2 quite a while a now and was wondering why i am not able to access the objects in ngOnInit() but they are accessible in the service call. 我正在研究angular2现在很长一段时间,我想知道为什么我无法访问ngOnInit()的对象,但它们可以在服务调用中访问。 For ex. 对于前者

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
    }   
}

So I am curious to know how can I make this accessible in ngOnInit? 所以我很想知道如何在ngOnInit中访问它?

It's because this.globalObjectsService.selectedWorkspace is set asynchronously within the subscribe callback. 这是因为this.globalObjectsService.selectedWorkspace是在订阅回调中异步设置的。 The component doesn't wait for asynchronous processing triggered in its constructor to be complete before executing the ngOnInit hook method. 在执行ngOnInit钩子方法之前,组件不会等待在其构造函数中触发的异步处理完成。

See: 看到:

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

The ngOnInit method "only" takes part in the component lifecycle. ngOnInit方法“仅”参与组件生命周期。

Edit 编辑

If you want to trigger processing based on the selectedWorkspace property, you could execute them within the subscribe callback: 如果要基于selectedWorkspace属性触发处理,可以在subscribe回调中执行它们:

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

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

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

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