AngularJS 2例外:错误:未捕获(按承诺):TypeError:无法将属性"company"设置为null [英] AngularJS 2 EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property 'company' of null

查看:71
本文介绍了AngularJS 2例外:错误:未捕获(按承诺):TypeError:无法将属性"company"设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在坚持,不知道为什么.这是组件的代码:

I stuck for all day and don't know why. This is the code of component:

import {Component, ViewEncapsulation, OnInit} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {Company} from './company';
import {Job} from './job'
import {CompanyService} from './company-service';

@Component({
  directives: [
  ROUTER_DIRECTIVES,
  ],
  selector: '[job-list-page]',
  host: {
    class: 'job-list-page app'
  },
  template: require('./job-list-page.html'),
  encapsulation: ViewEncapsulation.None,
  styles: [require('./job-list-page.scss'), require('./login-page.scss')],
  providers: [CompanyService]
})


export class JobListPage implements OnInit{
  company: Company;
  constructor(private companyService: CompanyService){}

  ngOnInit(){
    this.getCompany();
  }

  getCompany(){
    this.companyService.getCompany().then(function(company){
      console.log(company);
      this.company = company;
      }
    );
  }
}

这是company-service.ts的代码:

This is the code of company-service.ts:

import { Injectable } from '@angular/core';
import { Company } from './company';
import { Job } from './job'
import { COMPANY } from './mock-company';

@Injectable()
export class CompanyService{
    getCompany(){
        return Promise.resolve(COMPANY);
    }
}

我在getCompany()中得到了对象.但是我无法将对象分配给this.company.它将显示错误.我不知道为什么看来我无法在then()方法中为该变量分配任何值.

I got the object in the getCompany(). But I can't assign the object to the this.company. It will show the error. I don't know why. It looks like I can't assign any value to the variable in the then() method.

以下是console.log:

the following is the console.log:

Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
job-list-page.ts:33 Object {id: "invision", name: "INVISONAPP", logo: "assets/images/pictures/invision-logo-square.png", description: "InVision is the world's leading design collaborati…ernote, Twitter, Adobe, Salesforce and many more.", jobs: Array[1]}
browser_adapter.ts:88 EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property 'company' of null
browser_adapter.ts:78 EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property 'company' of nullBrowserDomAdapter.logError @ browser_adapter.ts:78BrowserDomAdapter.logGroup 

真的需要帮助,谢谢.

推荐答案

您应该使用箭头函数才能在回调中使用词汇this:

You should use an arrow function to be able to use the lexical this in the callback:

getCompany(){
  this.companyService.getCompany().then((company) => {
    console.log(company);
    this.company = company;
    }
  );
}

这篇关于AngularJS 2例外:错误:未捕获(按承诺):TypeError:无法将属性"company"设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆