3.16

53 阅读1分钟

vue父子传值

首先注册import HappyA from './components/HappyA.vue'

components: { HappyA }

记得在父的div中使用子组件,然后在父组件中data:function()里面return一个值msg

在HappyA中加上 :msg="msg"

在子组件中的div内使用这个{{msg}},然后在script中使用props:['msg']这样就完成了

需要修改的话首先在子组件中添加点击事件change,methods中 change:function(){ this.$emit('handler') }

在父组件中加上子传过来的@handler='handler',在methods中 handler:function(){ this.msg="父被子修改了" }完成修改

图片 两种,第一种可直接在css中写backgound图片的地址。

第二种在div中加上:style,使用拼接的方式写出图片的地址:style="{background:'url('+require(./assets/1.jpg)+') no-repeat'}"