我看到一個奇怪的錯誤,我嘗試從另一個方法調用方法,並且這兩個方法都在同一個Angular 2 TypeScript組件中。如何從同一個Angular(Typescript)組件類中的另一個方法調用一個方法?
在下面的代碼中看到,當調用ngOnInit()時,它將啓動method1。方法1試圖調用this.method2()。這是問題所在。 出現的錯誤如下: 錯誤:無法讀取未定義的屬性'method2'
爲什麼此對象未定義?我怎樣才能解決這個問題,以便我可以用像Java這樣的語言在同一個類中調用屬於某個類的方法?
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'my-dashboard',
moduleId: module.id,
templateUrl: './dashboard.component.html',
styleUrls: [ './dashboard.component.css' ]
})
export class DashboardComponent implements OnInit {
constructor() {
}
ngOnInit(): void {
this.method1();
};
method1(): void {
this.method2();
};
method2(]): void {
console.log("hi");
}
}
是的]一個錯字?在method2定義 –
是的,應該工作,否則。我能看到的代碼沒有錯。 – pixelbits