Echarts 点击事件,this无法正确获取到对应的属性,this的指向问题

Echarts 点击事件,this无法正确获取到对应的属性,this的指向问题

Echarts 点击事件

<template>
  <div class="radiopie">
    <div ref="radiopieChart" class="radiopieChart"></div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import echarts from "echarts";
export default {
  name: "pieInfo",
  props: {
    question: {},
  },
  data() {
    return {
      datatitle: ["是", "否"],
      datainfo: [],
      isshow: false,
      chart: null,
      status: "",
      gridNo: "",
      showModel: false,
    };
  },
  methods: {
    find() {
      this.datainfo.push({
        value: this.question.values["是"],
        name: "是",
        text: this.question.key,
      });
      this.datainfo.push({
        value: this.question.values["否"],
        name: "否",
        text: this.question.key,
      });
      this.chart = echarts.init(this.$refs.radiopieChart);
      const option = {
        title: {
          top: 20,
          text: this.question.key,
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          bottom: 0,
          left: "center",
          data: this.datatitle,
        },
        series: [
          {
            name: "数据",
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: this.datainfo,
            label: {
              show: false,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
      this.chart.on("click", function (param) {
       console.log("param", param);
      });
    },
  },
  mounted() {
    this.find();
  },
  created() {},
};
</script>

<style  lang="less" scoped>
.radiopie {
  width: 19%;
  height: 280px;
  margin-bottom: 30px;
}
.radiopieChart {
  height: 280px;
}
</style>

Echarts 点击事件,this的指向问题

<template>
  <div class="radiopie">
    <div ref="radiopieChart" class="radiopieChart"></div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import echarts from "echarts";
export default {
  name: "pieInfo",
  props: {
    question: {},
  },
  data() {
    return {
      datatitle: ["是", "否"],
      datainfo: [],
      isshow: false,
      chart: null,
      status: "",
      showModel: false,
    };
  },
  methods: {
    find() {
      this.datainfo.push({
        value: this.question.values["是"],
        name: "是",
        text: this.question.key,
      });
      this.datainfo.push({
        value: this.question.values["否"],
        name: "否",
        text: this.question.key,
      });
      this.chart = echarts.init(this.$refs.radiopieChart);
      const option = {
        title: {
          top: 20,
          text: this.question.key,
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          bottom: 0,
          left: "center",
          data: this.datatitle,
        },
        series: [
          {
            name: "数据",
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: this.datainfo,
            label: {
              show: false,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
        this.chart.setOption(option);
      //解决办法一,提前定义this
       let that = this;
      this.chart.on("click", function (param) {
       console.log("param", param);
       //输出为空
       this.status="123"
       console.log("this.status"this.status)
        //输出为123
        that .status="123"
       console.log("that.status"that.status)
      });
      //解决办法二,用=》替换function
         this.chart.on("click", param => {
        console.log("status"this.status)
      });
    },
  },
  mounted() {
    this.find();
  },
  created() {},
};
</script>

<style  lang="less" scoped>
.radiopie {
  width: 19%;
  height: 280px;
  margin-bottom: 30px;
}
.radiopieChart {
  height: 280px;
}
</style>
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值