vue父组件向子组件传递数值 props

vue 组件操作避免不了传值的问题,传值如何操作,项目中笔记记录点滴

项目原型创建绘图组件,实现组件随处引入,这就涉及到,值也是可变的,所以需要父类传值。

[外链图片转存失败(img-DNHoNzUq-1562913189358)(/Users/admin/Library/Application Support/typora-user-images/image-20190712141749895.png)]

  • 1.在父组件中将值设置

[外链图片转存失败(img-HiQI5G6b-1562913189361)(/Users/admin/Library/Application Support/typora-user-images/image-20190712142147479.png)]

  • 2.传递值

[外链图片转存失败(img-FMJMHNWN-1562913189362)(/Users/admin/Library/Application Support/typora-user-images/image-20190712142616891.png)]

  • 3.在子组件中使用props接受父组件传递的值

[外链图片转存失败(img-foh6mcXN-1562913189363)(/Users/admin/Library/Application Support/typora-user-images/image-20190712142410910.png)]

记录一下自己的开发,也希望给到刚刚学习的人一些帮助,下面贴上代码

  • 父组件
<template>
    <div>
        <el-row :gutter="20">
            <el-col>
                <Schart :polar="polar"></Schart>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import Schart from './chart/Line';
    export default {
        name: 'dashboard',
        data() {
            let datas = []

            for (let i = 0; i <= 360; i++) {
                let t = i / 180 * Math.PI
                let r = Math.sin(2 * t) * Math.cos(2 * t)
                datas.push([r, i])
            }

            return {
                polar: {
                    title: {
                        text: '极坐标双数值轴'
                    },
                    legend: {
                        data: ['line']
                    },
                    polar: {
                        center: ['50%', '54%']
                    },
                    tooltip: {
                        trigger: 'item',
                        axisPointer: {
                            type: 'cross'
                        }
                    },
                    angleAxis: {
                        type: 'value',
                        startAngle: 0
                    },
                    radiusAxis: {
                        min: 0
                    },
                    series: [
                        {
                            coordinateSystem: 'polar',
                            name: 'line',
                            type: 'line',
                            showSymbol: false,
                            data: datas
                        }
                    ],
                    animationDuration: 2000
                },
            }
        },
        components: {
            Schart
        },
   
    }
</script>

  • 2.子组件
<template>
    <v-chart :options="polar"/>
</template>
<script>
    import ECharts from 'vue-echarts'
    import 'echarts/lib/chart/line'
    import 'echarts/lib/component/polar'

export default {
    name:'Line',
    data() {

    },
    props:{
        polar: {
            type:Array,
            required:true
        }
    },
    components: {
        'v-chart': ECharts
    },
}
</script>
posted @ 2019-07-12 20:33  荇菜  阅读(9714)  评论(0编辑  收藏  举报