Jac*_*kie 6 javascript nvd3.js angular angular4-router
我已经NVD3在Angular 4中实现了Charts.在回调函数内部写了一个Click事件,点击图表我试图导航到另一个组件但是我无法导航.
代码:
import { Router} from '@angular/router';
export class MyNewComponentComponent implements OnInit {
constructor(public router: Router)
{
}
this.options = {
chart: {
type: 'discreteBarChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 50,
left: 55
},
x: function(d){return d.label;},
y: function(d){return d.value;},
showValues: true,
valueFormat: function(d){
return d3.format(',.4f')(d);
},
duration: 500,
xAxis: {
axisLabel: 'X Axis'
},
yAxis: {
axisLabel: 'Y Axis',
axisLabelDistance: -10
},
callback: function(chart){
chart.discretebar.dispatch.on('elementClick', (angularEvent,e) => {
console.log("Inside click");
this.router.navigate(["/app-new-component2"]);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
等待建议.提前致谢 ..
所以你的问题就在这里
callback: function(chart){ // note the callback function
chart.discretebar.dispatch.on('elementClick', (angularEvent,e) => {
console.log("Inside click");
this.router.navigate(["/app-new-component2"]);
});
}
Run Code Online (Sandbox Code Playgroud)
因此,在您的回调所在的位置,您使用的是es5 function(),这意味着该函数中的任何内容都不会占用全局范围this,而是创建一个新的范围.因此,在这种情况下,如果this.router.navigate你没有引用组件(全局this),那么你引用的函数范围this就没有了router.所以你想要做的就是这个,
callback: (chart) => {
chart.discretebar.dispatch.on('elementClick', (angularEvent,e) => {
console.log("Inside click");
this.router.navigate(["/app-new-component2"]);
});
}
Run Code Online (Sandbox Code Playgroud)
使用ES6(胖箭头)功能() => {}将保留全局范围this,让您使用路由器.
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |