我試圖將點擊事件添加到highcharts欄以導航到另一個html視圖並加載另一個組件。我現在從控制檯的錯誤是:導航未定義的路由器角2中的錯誤
Cannot read property 'navigate' of undefined
這是我在我的組件文件:
import { Router } from '@angular/router';
constructor(private myService: MyService, private router: Router) {}
plotOptions: {
bar: {
dataLabels: {
enabled: false
}
},
series: {
pointWidth: 15,
cursor: 'pointer',
point: {
events: {
click: function(){
this.router.navigate(['/nestedFolder/newPage.component.html']);
}
}
}
}
}
我不知道我完全理解爲什麼我收到這個錯誤還是我我正在使用錯誤的方式導航。
文件夾結構:
- 主文件夾
- main.component.ts
- main.component.html
- nestedFolder
- newPage.component.html
- newPage.component.ts
您是否在構造函數中創建了路由器實例? –
@AuuragSinghBisht是的,我創建它(我更新了我的帖子,以顯示它) – bluePearl
'這'內部回調並不是指組件 - 你必須綁定'this'dynamicaly - click:function(){...} .bind(this) – morganfree