vue 组件传值、页面(路由)传值
时间:2020-07-17 19:58:29
收藏:0
阅读:40
一、组件传值
1、父组件——>子组件(属性传值)
在父组件页面引入子组件并在,儿子身上绑定属性 :list = "list" ;
父组件页面data中填写要传递的值 list[a,b,c],
子组件页面接受
props: {
list: {
type: Array,
default() {
return [];
}
}
}
子组件页面就可以拿到值了 {{list[0]}}
2、子组件——》父组件(事件传值)
子组件中定义事件:
<div :class="[‘btn‘, show ? ‘btn1‘ : ‘‘]" @click="showMenu"></div>;
通过$emit触发,第一个参数是事件名 第二个是要传递的数据;
showMenu() {
this.show = !this.show;
this.$emit(‘showMenu‘, this.show);
},
在父组件页面引入子组件并在,儿子身上绑定 触发的事件名 @showMenu="toShowMenu";
通过参数接受传过来数据
toShowMenu(v) {
console.log(v)
}
原文:https://www.cnblogs.com/jervy/p/13331636.html
评论(0)