Vue中Echarts事件绑定中实现路由跳转:【解决报错】TypeError: Cannot read property ‘push‘ of undefined

Echarts3之后的版本事件绑定的函数书写如下:

this.myChart.on("click", function (params) {
    var url = "/";
    console.log(this.$router)
    this.$router.push(url);
});

这里汇报错,在控制台中查看日志可知:this.$router属性为undefined。

这里是由于创建一个函数之后this的对象已经不再指向vue了。

所以我们利用回调函数来进行书写。

this.myChart.on("click", (params) => {
    // 这里必须要使用回调函数
    var url = "/";
    console.log(this.$router);
    this.$router.push(url);
});

可以看到这样就可以正常跳转页面了,并且输出了this.$router对象。

你可能感兴趣的:(echarts,javascript,前端,vue.js,vue)