• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 知识库 知识库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

2022年02月05日发布vue传值有哪8种方法

武飞扬头像
xhjyxxw
帮助0

知行礼动

大家好,今日小科来聊聊一篇关于2022年02月05日整理发布:vue传值有哪8种方法的文章,现在让我们往下看看吧!

传递值的方法有props和$emit、$ attrs和$listeners、中央事件总线、v-model、provide和inject、$parent和$children、vuex和localStorage/session。

本教程的操作环境:windows7系统,Vue版本vue2.9.6,DELL G3电脑。

对于vue来说,组件之间的消息传输非常重要。以下是我对组件之间消息传输的常见方式的总结。

道具和$emit(通用)

$attrs和$ listeners

中央事件总线(非父子组件之间的通信)

v型

提供并注入

$家长和$孩子

状态管理

1、道具和$emit

父组件通过prop将数据传输给子组件,子组件通过$emit触发器事件将数据传输给父组件。

Vue.component('child ',{ data(){ return { mymessage : this . message } },Template :`div输入类型=' text ' v-model=' my message ' @ input=' passData(my message)'/div `,props : ['message'],//设置props属性的值,Get data methods : { passData(val){//在父组件中触发事件,并将值传递给此。$ emit ('getchilddata ',val) }}}) vue.component ('parent ',{ template3360 ' div这是父组件!/p child : message=' message ' v-: getChildData=' getChildData '/child/div `,data(){ return { message : ' hello ' } },Methods:{ //执行事件getChildData(val){ console . log(val)} } })在上面的示例中,有父组件和子组件。

1)父组件将消息数据传递给子组件,通过v-on绑定一个getChildData事件来监控子组件的触发事件;

2)子组件通过道具获取相关消息数据,最后通过这个触发getChildData事件。$emit

2美元、attrs和$ listeners

第一种处理父子组件间数据传输的方式有一个问题:如果父组件A下有一个子组件B,组件B下有一个组件C,那么如果组件A想把数据传递给组件C呢?如果采用第一种方法,我们必须让组件A通过prop向组件B传递消息,组件B通过prop向组件C传递消息;如果组件A和组件C之间的组件比较多,那么采用这种方法就非常复杂。Vu2.4开始提供$ attrs和$ listeners来解决这个问题,使组件A能够将消息传递给组件c。

Vue.component('C ',{ 0

模板: `

差异

输入类型=' text ' v-model=' $ attrs . message c ' @ input=' passCData($ attrs . message c)'/div

`,

methods:{

passCData(val){ 0

//触发父组件A中的事件

这个。$emit('getCData ',val)

}

}

})

Vue.component('B ',{ 0

数据(){ 0

turn { mymessage:this.message } }, template:` <div> <input type="text" v-model="mymessage" @input="passData(mymessage)"> <!-- C组件中能直接触发getCData的原因在于 B组件调用C组件时 使用 v-on 绑定了$listeners 属性 --> <!-- 通过v-bind 绑定$attrs属性,C组件可以直接获取到A组件中传递下来的props(除了B组件中props声明的) --> <C v-bind="$attrs" v-on="$listeners"></C> </div> `, props:['message'],//得到父组件传递过来的数据 methods:{ passData(val){ //触发父组件中的事件 this.$emit('getChildData',val) } } }) Vue.component('A',{ template:` <div> <p>this is parent compoent!</p> <B :messagec="messagec" :message="message" v-on:getCData="getCData" v-on:getChildData="getChildData(message)"></B> </div> `, data(){ return { message:'hello', messagec:'hello c' //传递给c组件的数据 } }, methods:{ getChildData(val){ console.log('这是来自B组件的数据') }, //执行C子组件触发的事件 getCData(val){ console.log("这是来自C组件的数据:" val) } } })

3、中央事件总线

上面两种方式处理的都是父子组件之间的数据传递,而如果两个组件不是父子关系呢?这种情况下可以使用中央事件总线的方式。新建一个Vue事件bus对象,然后通过bus.$emit触发事件,bus.$on监听触发的事件。

Vue.component('brother1',{ data(){ return { mymessage:'hello brother1' } }, template:` <p> <p>this is brother1 compoent!</p> <input type="text" v-model="mymessage" @input="passData(mymessage)"> </p> `, methods:{ passData(val){ //触发全局事件globalEvent bus.$emit('globalEvent',val) } } }) Vue.component('brother2',{ template:` <p> <p>this is brother2 compoent!</p> <p>brother1传递过来的数据:{{brothermessage}}</p> </p> `, data(){ return { mymessage:'hello brother2', brothermessage:'' } }, mounted(){ //绑定全局事件globalEvent bus.$on('globalEvent',(val)=>{ this.brothermessage=val; }) } }) //中央事件总线 var bus=new Vue(); var app=new Vue({ el:'#app', template:` <p> <brother1></brother1> <brother2></brother2> </p> ` })

4、provide和inject

在 Vue.js 的 2.2.0 版本中添加加了 provide 和 inject 选项。他们成对出现,用于父级组件向下传递数据。

父组件中通过provider来提供变量,然后在子组件中通过inject来注入变量。不论子组件有多深,只要调用了inject那么就可以注入provider中的数据。而不是局限于只能从当前父组件的prop属性来获取数据,只要在父组件的生命周期内,子组件都可以调用。

Vue.component('child',{ inject:['for'],//得到父组件传递过来的数据 data(){ return { mymessage:this.for } }, template:`}) Vue.component('parent',{ template:`this is parent compoent!`, provide:{ for:'test' }, data(){ return { message:'hello' } } })

5、v-model

  父组件通过v-model传递值给子组件时,会自动传递一个value的prop属性,在子组件中通过this.$emit(‘input',val)自动修改v-model绑定的值

Vue.component('child',{ props:{ value:String, //v-model会自动传递一个字段为value的prop属性 }, data(){ return { mymessage:this.value } }, methods:{ changeValue(){ this.$emit('input',this.mymessage);//通过如此调用可以改变父组件上v-model绑定的值 } }, template:` <p> <input type="text" v-model="mymessage" @change="changeValue"> </p> }) Vue.component('parent',{ template:` <p> <p>this is parent compoent!</p> <p>{{message}}</p> <child v-model="message"></child> </p> `, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` <p> <parent></parent> </p> ` })

6、$parent和$children

在组件内部可以直接通过子组件$parent对父组件进行操作,父组件通过$children对子组件进行操作.

Vue.component('child',{ props:{ value:String, //v-model会自动传递一个字段为value的prop属性 }, data(){ return { mymessage:this.value } }, methods:{ changeValue(){ this.$parent.message = this.mymessage;//通过如此调用可以改变父组件的值 } }, template:` <p> <input type="text" v-model="mymessage" @change="changeValue"> </p> }) Vue.component('parent',{ template:` <p> <p>this is parent compoent!</p> <button @click="changeChildValue">test</button > <child></child> </p> `, methods:{ changeChildValue(){ this.$children[0].mymessage = 'hello'; } }, data(){ return { message:'hello' } } }) var app=new Vue({ el:'#app', template:` <p> <parent></parent> </p> ` })

7、vuex处理组件之间的数据交互

如果业务逻辑复杂,很多组件之间需要同时处理一些公共的数据,这个时候才有上面这一些方法可能不利于项目的维护,vuex的做法就是将这一些公共的数据抽离出来,然后其他组件就可以对这个公共数据进行读写操作,这样达到了解耦的目的。

8、localStorage / sessionStorage

这种通信比较简单,缺点是数据和状态比较混乱,不太容易维护。

通过window.localStorage.getItem(key) 获取数据

通过window.localStorage.setItem(key,value) 存储数据

注意用JSON.parse() / JSON.stringify() 做数据格式转换

localStorage / sessionStorage可以结合vuex,实现数据的持久保存,同时使用vuex解决数据和状态混乱问题。

这篇好文章是转载于:知行礼动

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 知行礼动
  • 本文地址: /news/detail/tanhbgggcg