首先在子组件中接受修改后的值
props: {
currentIndex: {
type: [Number, String],
default: 1,
},
},
利用 $emit调用父组件changeIndex方法,将改变的值传送置父组件
methods: {
// 子组件改变父组件的值
changeTabs(index) {
this.$emit("changeIndex", index);
// this.currentIndex = index;
// console.log(this.currentIndex);
},
},
在父组件中创建changeIndex方法,接受该值
export default {
data() {
return {
currentIndex: 0, //父组件中修改的值
};
},
methods: {
changeIndex(val) {
this.currentIndex = val;
},
},
};
在父组件中调用该子组件
<hk-tabs :currentIndex="currentIndex" @changeIndex="changeIndex"></hk-tabs>
参考效果如图所示,点击不同tab改变