【问题标题】:typescript Cannot find name variable declare at class level inside function within same class打字稿在同一类的函数内部找不到名称变量声明
【发布时间】:2016-10-28 22:11:19
【问题描述】:

需要有关我正在尝试学习的以下代码的建议。我正在使用 typescript 1.8.10 编译器和 EM5:

module myAwersome.module{
    'use strict';
    export interface INavigateService {
            links: string[],
            getLinks():void
            }
    class Test implements INavigateService  {
            links:string[] = [];
            getLinks():void {
                links.push("hello world"); //ERROR unable to find name links 
            }
}

【问题讨论】:

  • 使用this.links解决
  • 当 Test 类被转译为 JavaScript 时,'links' 属性在函数构造函数中使用 this.links = []; 进行初始化;

标签: function class variables typescript interface


【解决方案1】:

当您访问班级成员时,您必须通过 this 关键字进行访问:

this.links.push("Hello world");

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2017-06-10
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多