vue父组件向子组件传递数值 props
vue 组件操作避免不了传值的问题,传值如何操作,项目中笔记记录点滴
项目原型创建绘图组件,实现组件随处引入,这就涉及到,值也是可变的,所以需要父类传值。
- 1.在父组件中将值设置
- 2.传递值
- 3.在子组件中使用props接受父组件传递的值
记录一下自己的开发,也希望给到刚刚学习的人一些帮助,下面贴上代码
- 父组件
<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>