在全局对象之外读取 var

Posted

技术标签:

【中文标题】在全局对象之外读取 var【英文标题】:Read var outside of global object 【发布时间】:2018-06-05 02:13:44 【问题描述】:

在我的项目中,我使用的是kendo-ui。因此在ngOnInit生命周期Hook 我正在尝试创建这样的东西:

ngOnInit() 
  let hello = 600;
  public bulletData: any[] = [200, this.hello];
  public bulletValueAxis: any = 
     min: 0,
     max: this.hello
  ;

它不起作用!

全局对象和数组看不到外部的 var。有谁知道我该如何解决这个问题?

【问题讨论】:

只要把你好删除this.hello ERROR ReferenceError: hello is not defined 【参考方案1】:

hello变量没有定义为组件的属性,所以它不属于this. 还要从ngOnInit 中声明您的组件属性。 试试这个:

Working plunkr

public bulletData: any[];
public bulletValueAxis: any;

constructor() 

ngOnInit() 
     let hello = 600;
     this.bulletData = [200, hello];
     this.bulletValueAxis = 
       min: 0,
       max: hello
    ;

【讨论】:

我试过了。首先,我在 ngOnInit 或 ngDoCheck 中声明了 hello,然后在下一步中我使用了您的代码。控制台给我写了一个错误“ERROR ReferenceError: hello is not defined” @komandir87 检查我的更新答案,还包括一个有效的 plunkr

以上是关于在全局对象之外读取 var的主要内容,如果未能解决你的问题,请参考以下文章