当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 详解VUE里子组件如何获取父组件动态变化的值

详解VUE里子组件如何获取父组件动态变化的值

2019年05月29日  | 移动技术网IT编程  | 我要评论

微蓝晴雨表,v-line,3u8876

在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 lang="stylus" 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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网