vue props 传递函数
Props的type是函数通过 props 传递 函数 看看有啥用。// 父组件</template><div><children :add='childrenClick'></children><p>{{countF}}</p></div></template>...
·
Props的type是函数
通过 props 传递 函数 看看有啥用。
// 父组件
</template>
<div>
<children :add='childrenClick'></children>
<p>{{countF}}</p>
</div>
</template>
<script>
import children from './Children'
export default {
name: 'HelloWorld',
data() {
return {
countF: 0,
}
},
methods: {
childrenClick(c){
this.countF += c;
}
},
components:{
children,
}
}
</script>
// 子组件
<template>
<div>
<button @click="handClick(count)">点击加 1 </button>
</div>
</template>
<script>
export default {
data() {
return {
count:1,
}
},
props:{
add:{
type: Function
}
},
methods: {
handClick(){
this.add( ++this.count); // 父组件方法
}
},
}
可以看到 chirden 组件在中 使用 props.add() , 调用的是 父组件的方法。
更多推荐
已为社区贡献1条内容
所有评论(0)