Angular 2分配问题

时间:2017-01-18 07:37:02

标签: javascript angular typescript

我正在尝试分配格式化的值,下面的代码正在按顺序

export class AppComponent {
  public urlObj = this._urlService.get('GET_CLIENT_ID');
  url = this._urlService.formatURL('root', this.urlObj.URL);
  constructor(private _urlService: UrlService) {
  }
 }

但是当我尝试直接更改urlObj.URL时,它不像

那样工作
export class AppComponent {
  public urlObj = this._urlService.get('GET_CLIENT_ID');
  this.urlObj.URL = this._urlService.formatURL('root', this.urlObj.URL);
  constructor(private _urlService: UrlService) {
  }
}

这是Unexpected token at this.urlObj

1 个答案:

答案 0 :(得分:2)

您的_urlService在构造函数级别初始化。尝试在其中进行赋值操作:

export class AppComponent {
  public urlObj; 

  constructor(private _urlService: UrlService) {
   this._urlService.get('GET_CLIENT_ID');
   this.urlObj.URL = this._urlService.formatURL('root', this.urlObj.URL);
  }