【发布时间】:2018-12-18 19:06:04
【问题描述】:
有什么方法可以在不使用this. 的情况下从StartPage 类访问变量MyVariable?我问的原因是我需要在函数中引用它,而this 在那里不起作用。我从here 知道我可以使用=> {...,但这并不总是适合我。我该怎么做?
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-start',
templateUrl: './start.page.html',
styleUrls: ['./start.page.scss'],
})
export class StartPage implements OnInit {
constructor() {}
MyVariable = "Value";
ngOnInit() {
console.log(MyVariable);
//anyway to use MyVariable without `this.` prefix?
}
}
【问题讨论】:
-
bind(this)适合你吗? -
您是否尝试将其传递给
ngOnInit? -
The reason I am asking is that I need to reference this in a functionthis,或者您可能正在使用 lambda 但您不知道如何使用。 -
考虑使用
this。你的队友和未来的自己会感谢你。如果您在使用特定箭头功能时遇到问题,请发布它的代码,我们可以提供帮助。不应该出现箭头函数“不起作用”的情况。
标签: javascript angular typescript