【Vue】子组件监听父页面值得变化

13 篇文章 0 订阅

效果图

其中环装图为子组件,滑块为父页面元素

目录

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import echarts from 'echarts'

import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI);
Vue.config.productionTip = false
Vue.prototype.$echarts=echarts;
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

组件MyChart.vue

<template>
  <div class="my-chart" ref="chart">

  </div>
</template>

<script>
    export default {
        name: "MyChart",
        mounted() {

            this.myChart = this.$echarts.init(this.$refs.chart);
            this.changeChart();
        },
        data() {
            return {
                myChart: null
            }
        },
        methods: {
            changeChart() {
                var option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c} ({d}%)"
                    },
                    legend: {
                        show: false
                    },
                    series: [
                        {
                            type: 'pie',
                            radius: ['50%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                normal: {
                                    show: false,
                                    position: 'center'
                                },

                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                            data: [
                                {value: this.a, name: 'A'},
                                {value: this.b, name: 'B'},
                                {value: this.c, name: 'C'},
                                {value: this.d, name: 'D'},
                            ]
                        }
                    ]
                };
                this.myChart.setOption(option);
            }
        },
        props: ["a", 'b', 'c', 'd'],
        watch: {
            a(oldValue, newValue) {
                this.changeChart();
            },
            b(oldValue, newValue) {
                this.changeChart();
            },
            c(oldValue, newValue) {
                this.changeChart();
            },
            d(oldValue, newValue) {
                this.changeChart();
            }
        }
    }
</script>

<style scoped>
  .my-chart {
    width: 100%;
    height: 100%;
  }
</style>

父页面HelloWorld.vue

<template>
  <div class="hello">
    <div>
      <div>A
        <div>
          <el-slider v-model="a"></el-slider>
        </div>
      </div>
      <div>B
        <div>
          <el-slider v-model="b"></el-slider>
        </div>
      </div>
      <div>C
        <div>
          <el-slider v-model="c"></el-slider>
        </div>
      </div>
      <div>D
        <div>
          <el-slider v-model="d"></el-slider>
        </div>
      </div>
    </div>
    <div>
      <div v-for="item in 5" :key="item">
        <my-chart :a="a" :b="b" :c="c" :d="d"></my-chart>
      </div>
    </div>
  </div>
</template>

<script>
    import MyChart from "./MyChart";

    export default {
        name: 'HelloWorld',
        components: {MyChart},
        data() {
            return {
                a: 10,
                b: 10,
                c: 10,
                d: 10
            }
        },
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .hello > div {
    display: flex;
  }

  .hello > div:nth-child(1) > div {
    display: flex;
    align-items: center;
    margin-left: 50px;
  }

  .hello > div:nth-child(1) > div > div {
    width: 200px;
    margin-left: 20px;
  }

  .hello > div:nth-child(2) {
    height: 200px;
  }

  .hello > div:nth-child(2) > div {
    width: 200px;
  }
</style>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值