vue 父子组件双向绑定例子

父组件:

<template>
  <div class="hello">
    {{ pageNum }}父亲
    <child :pagea.sync="pageNum"></child>
    <input v-model="pageNum"/>父亲
  </div>
</template>

<script>
import child from './child';
export default {
  name: 'HelloWorld',
  components: {child},
  data () {
    return {
      pageNum: '1'
    };
  },
  methods: {
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

子组件:

<template>
  <div>{{currentPage}}子
    <input v-model="currentPage"/></div>

</template>

<script>
export default {
  name: 'child',
  props: { /**
     * 微应用key
     */
    pagea: {
      type: String,
      default: '1'
    }},
  computed: {
    currentPage: {
      get () {
        return this.pagea;
      },
      set (val) {
        this.$emit('update:pagea', val);
      }
    }
  },
  mounted () {
  }
};
</script>

<style scoped>

</style>

 

posted @ 2022-07-15 18:30  陈扬天  阅读(101)  评论(0编辑  收藏  举报