Typescript - 如何访问基础 class 属性
Typescript - How to access base class properties
我知道还有其他一些关于该主题的问题,但我的问题与他们不一样:我检查了我的 class 并且我已经以与他们相同的方式使用它。
我将 class A 扩展到 class B,但我无法访问 B 中的 A public 属性。这是我的(简化)代码:
export class A {
propertyA: string;
constructor() {
this.propertyA = "some text";
}
}
import {A} from "./A";
export class B extends A {
constructor() {
super();
}
static method() {
console.log(this.propertyA);
}
}
您不能从静态方法访问 this
。删除 static
它应该可以工作。
我知道还有其他一些关于该主题的问题,但我的问题与他们不一样:我检查了我的 class 并且我已经以与他们相同的方式使用它。 我将 class A 扩展到 class B,但我无法访问 B 中的 A public 属性。这是我的(简化)代码:
export class A {
propertyA: string;
constructor() {
this.propertyA = "some text";
}
}
import {A} from "./A";
export class B extends A {
constructor() {
super();
}
static method() {
console.log(this.propertyA);
}
}
您不能从静态方法访问 this
。删除 static
它应该可以工作。