aurelia observerlocator onchange 找不到变量

aurelia observerlocator onchange does not find variables

我有以下代码,但是当 onChange 被触发时它给我一个错误。 (未捕获的类型错误:无法设置未定义的 属性 'myVar')

import { inject } from 'aurelia-framework';
import { ObserverLocator } from 'aurelia-framework';

@inject(ObserverLocator)
export class myClass{
    field= "";
    myVar = 0;
    constructor(observerLocator) {
        this.field= "";
        var subscription = observerLocator
                .getObserver(this, 'field')
                .subscribe(this.onChange);

    }

    onChange(){
        this.myVar +=1;
    }

}

范围问题,试试

var subscription = observerLocator
            .getObserver(this, 'field')
            .subscribe(this.onChange.bind(this));