vue父组件向子组件传递数据
父组件 food 通过 props 把 值为 0 的 type 字段传给子组件,子组件在初始化时可以拿到 type 字段,渲染出字符“0 水果”
// 父组件 food.vue
<template>
<apple :type="type"></apple>
</template>
<script>
data (){
return {
type: 0
};
}
</script>
// 子组件 apple.vue
<template>
<span>{{childType}}</span>
</template>
<script>
props: ['type'],
created () {
this.childType = this.formatterType(this.type);
},
method () {
formatterType (type) {
if (type === 0) {
return "0 水果";
}
if (type === 1) {
return "1 蔬菜";
}
return '';
}
}
</script>

浙公网安备 33010602011771号