其他分享
首页 > 其他分享> > 组件v-model的用法

组件v-model的用法

作者:互联网

<!DOCTYPE html>
<div id="v-model-example" class="demo">
   <my-component v-model.capitalize="myText"></my-component>
      {{ myText }}
    </div>

<script src="https://unpkg.com/vue@next"></script>
<script>
    const app = Vue.createApp({   
       data() {
          return {
             myText: ''
             }
             }
             });
    app.component('my-component', {
       props: {
          modelValue: String,
          modelModifiers: {
             default: () => ({})
             }
             },
  emits: ['update:modelValue'],
  template: `
    <input type="text"
      :value="modelValue"
      @input="$emit('update:modelValue', $event.target.value)">
  `,
  created() {
    console.log(this.modelModifiers) // { capitalize: true }
  }
}).mount('#v-model-example')
</script>

错误的代码:

<!DOCTYPE html>
<div id="v-model-example" class="demo">
   <my-component v-model.capitalize="myText"></my-component>
      
    </div>

<script src="https://unpkg.com/vue@next"></script>
<script>
    const app = Vue.createApp({ });  
 
    app.component('my-component', {
       props: {
          modelValue: String,
          modelModifiers: {
             default: () => ({})
             }
             },
  emits: ['update:modelValue'],
  template: `
    <input type="text"
      :value="modelValue"
      @input="$emit('update:modelValue', $event.target.value)">
  `,
  created() {
    console.log(this.modelModifiers) // { capitalize: true }
  }
}).mount('#v-model-example')
</script>

标签:modelModifiers,true,app,component,用法,组件,model,modelValue
来源: https://blog.csdn.net/weixin_40945354/article/details/123089117