第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

VUE里子組件獲取父組件動(dòng)態(tài)變化的值

標(biāo)簽:
Vue.js

在VUE里父组件给子组件间使用props方式传递数据,但是希望父组件的一个状态值改变然后子组件也能监听到这个数据的改变来更新子组件的状态。

场景:子组件通过props获取父组件传过来的数据,子组件存在操作传过来的数据并且传递给父组件。

比如想实现一个switch开关按钮的公用组件:

 

 

1.父组件可以向按钮组件传递默认值。

2.子组件的操作可以改变父组件的数据。

3.父组件修改传递给子组件的值,子组件能动态监听到改变。

比如父组件点击重置,开关组件的状态恢复为关闭状态:

方法1:

1、因为存在子组件要更改父组件传递过来的数据,但是直接操作props里定义的数据vue会报错,所以需要在data里重新定义属性名并将props里的数据接收。

2、首先想到的肯定是在computed计算属性里监听数据的变化,那就直接在computed里监听父组件传递过来的props数据的变化,如果有变动就进行操作,如:

复制代码

export default {   name: 'SwitchButton',   props: {     status: {       type: Boolean,       default () {         return false       }     }   },   data () {     return {       switchStatusData: this.status // 重新定义数据     }   },   computed: {     switchStatus: function () {       return this.status // 直接监听props里的status状态     }   }   } }

复制代码

这样就可以在使用switchStatus的地方动态的监听到父组件status的变化。似乎只针对简单的数据类型有效。

方法2:

使用watch和computed组合实现:如

复制代码

export default {   name: 'SwitchButton',   props: {     status: {       type: Boolean,       default () {         return false       }     }   },   data () {     return {       switchStatusData: this.status     }   },   computed: {     switchStatus: function () {       return this.switchStatusData  // 监听switchStatusData 的变化     }   },   watch: {     status (newV, oldV) { // watch监听props里status的变化,然后执行操作       console.log(newV, oldV)       this.switchStatusData = newV     }   },   methods: {     switchHandleClick () {       this.switchStatusData = !this.switchStatusData       this.$emit('switchHandleClick', this.switchStatusData)     }   } }

复制代码

 

下面是实现该组件的代码:

复制代码

<template>   <span class="switch-bar" :class="{'active': switchStatus}" @click="switchHandleClick"><span class="switch-btn"></span></span> </template> <script> export default {   name: 'SwitchButton',   props: {     status: {       type: Boolean,       default () {         return false       }     }   },   data () {     return {       switchStatusData: this.status     }   },   computed: {     switchStatus: function () {       return this.status     }   },   // watch: {   //   status (newV, oldV) {   //     console.log(newV, oldV)   //     this.switchStatusData = newV   //   }   // },   methods: {     switchHandleClick () {       this.switchStatusData = !this.switchStatusData       this.$emit('switchHandleClick', this.switchStatusData)     }   } } </script> <style scoped>   @import "~styles/varibles.styl"   .area-wrapper     line-height: .8rem;     padding: 0 .4rem;     .switch       float: right;       font-size: 0;     .switch-bar       position: relative;       display: inline-block;       width: .8rem;       height: .4rem;       border-radius: .4rem;       background: #ddd;       border: 2px solid #ddd;       vertical-align: middle;       transition: background .3s, border .3s;       &.active         background: $bgColor;         border: 2px solid $bgColor;         .switch-btn           left: .4rem;           background: #fff;     .switch-btn       position: absolute;       left: 0px;       display: inline-block;       width: .4rem;       height: .4rem;       border-radius: .2rem;       background: #fff;       transition: background .3s, left .3s; </style>

复制代码

原文出处:https://www.cnblogs.com/amor17/p/10157313.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消