vue2和vue3 组件中v-model的使用方式

记录一下vue中v-mode在组件上的一些使用方式,看这一篇就够了

vue2基础使用

父组件

<template>
  <div>
    <h1>我是父组件:{{ count }}</h1>
    <Child v-model="count" />
    // 等价于
    //<Child :value='count' @input='count=$event' />
  </div>
</template>

<script>
import Child from '@/components/child'
export default {
  components: {
    Child
  },
  data() {
    return {
      count: 0
    }
  }
}
</script>

子组件

<template>
  <div>
    <h1>我是子组件: {{ value }}</h1>
    <button @click="clickHanlde">+1</button>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 0
    }
  },
  methods: {
    clickHanlde() {
      this.$emit('input', this.value + 1)
    }
  }
}
</script>

vue2中model自定prop和触发事件

父组件

<template>
  <div>
    <h1>我是父组件:{{ count }}</h1>
    <Child v-model="count" />
    // 等价于
    //<Child :count='count' @changeCount='count=$event' />
  </div>
</template>

<script>
import Child from '@/components/child'
export default {
  components: {
    Child
  },
  data() {
    return {
      count: 0
    }
  }
}
</script>

子组件

<template>
  <div>
    <h1>我是子组件: {{ count}}</h1>
    <button @click="clickHanlde">+1</button>
  </div>
</template>

<script>
export default {
  model: {
    prop: 'count',
    event: 'changeCount'
  },
  props: {
    count: {
      type: Number,
      default: 0
    }
  },
  methods: {
    clickHanlde() {
      this.$emit('changeCount', this.count + 1)
    }
  }
}
</script>

vue3中组件中使用v-model

父组件

<template>
  <div>
    <h1>我是父组件:{{ count }}</h1>
    <Child v-model="count" />
    <!-- <Child :modelValue="count" @update:modelValue="count = $event" /> -->
  </div>
</template>

<script>
import Child from './components/Child.vue'
import { ref } from 'vue'
export default {
  components: {
    Child
  },
  setup() {
    const count = ref(0)
    return { count }
  }
}
</script>

子组件

<template>
  <div>
    <h1>我是子组件: {{ modelValue }}</h1>
    <button @click="clickHanlde">+1</button>
  </div>
</template>

<script>
export default {
  props: {
    modelValue: {
      type: Number,
      default: 0
    },
  },
  emits: ['update:modelValue'],
  setup(props, context) {
    const clickHanlde = () => {
      context.emit('update:modelValue', props.modelValue + 1)
    }
    return { clickHanlde }
  }
}
</script>

vue3在组件上使用多个v-model

父组件

<template>
  <div>
    <h1>我是子组件: {{ modelValue }}--------{{ text }}</h1>
    <button @click="clickHanlde">+1</button>
  </div>
</template>

<script>
export default {
  props: {
    modelValue: {
      type: Number,
      default: 0,
    },
    text: {
      type: String,
      default: '',
    },
  },
  emits: ['update:modelValue', 'update:text'],
  setup(props, context) {
    const clickHanlde = () => {
      context.emit('update:modelValue', props.modelValue + 1);
      context.emit('update:text', props.text + 1);
    };
    return { clickHanlde };
  },
};
</script>

子组件

<template>
  <div>
    <h1>我是父组件:{{ count }}-------{{ msg }}</h1>
    <Child v-model="count" v-model:text="msg" />
  </div>
</template>

<script>
import Child from './components/Child.vue';
import { ref } from 'vue';
export default {
  components: {
    Child,
  },
  setup() {
    const count = ref(0);
    const msg = ref('');
    return { count, msg };
  },
};
</script>

vue3 script setup语法糖中使用v-model

父组件

<template>
 <h1>我是父组件:{{ count }}-------{{ msg }}</h1>
 <Child v-model="count" v-model:text="msg" />
</template>
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'

const count = ref(0)
const msg = ref('')
</script>

子组件

<template>
  <div>
    <h1>我是子组件: {{ modelValue }}--------{{ text }}</h1>
    <button @click="clickHanlde">+1</button>
  </div>
</template>

<script setup>
  const props = defineProps({
    modelValue: {
      type: Number,
      default: 0
    },
    text: {
      type: String,
      default: ''
    }
  })
  const  emits = defineEmits(['update:modelValue', 'update:text']);
  const clickHanlde = () => {
    emits('update:modelValue', props.modelValue + 1)
    emits('update:text', props.text + '1')
  }
</script>

<style></style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值