vue中.sync修饰符

117 阅读1分钟
  • 当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定。可以绑定多个属性。实现数据的双向绑定。
  • 在父组件data里定义一个变量page,子组件里改变这个变量并传给父组件。
  • 使用.async不需要写事件名称,需要更新谁,就直接绑定谁。
父组件:
<template>
  <v-pagination :page.sync="page"></v-pagination>
</template>
<script type="text/ecmascript-6">
  export default {
    data() {
      return {
        page: 1
      }
    }
  }  
</script>

子组件:
<template>
  <div class="pagination">
    <el-pagination
      background
      :pager-count="11"
      layout="prev, pager, next"
      @current-change="currentChange"
      :total="totalNum">
    </el-pagination>
  </div>
</template>
<script type="text/ecmascript-6">
  export default {
    methods: {
      currentChange(page) {
        this.$emit('update:page', page)
      }
    }
  }
</script>