vue 组件上v-model用法

父组件

<template>
  <div id="app">
    <span>{{ipt}}</span>
   <input type="text" @input="changeval" :vlaue="ipt">
   <span>{{ipt1}}</span>
   <input type="text" @input="ipt1=$event.target.value" :value="ipt1">
   <hr>
   <h4>{{count}}</h4>
    <!-- 父组件绑定v-module要传递得值 -->
   <Son v-model="count" ></Son>
   
  </div>
  
</template>
<script>
import Dau from "./components/dau.vue"
import Son from "./components/son"
    export default {
      data(){
        return {
          ipt:"",
          ipt1:"",
          count:1
        }
      },
      methods:{
        changeval(e){
            console.log(e.target.value);
            this.ipt = e.target.value
        }
      },
      components:{
        Son,Dau
      }
    }

</script>


<style>

</style>

子组件

<template>
    <div>
        {{this.value}}
        <button @click="fn">我是son组件的button</button>
    </div>
</template>

<script>
export default {

    props:["value"], // 通过v-model在子组件标签上接收到得值就叫value,且必须
    model: {    // model可以被必须叫叫input得事件名给起一个别名,在这里叫update
        prop: "value", //绑定的值,通过⽗组件传递
        event: "update" //⾃定义时间名
    },
     methods:{
         fn(){   // 自定义事件必须叫input
             this.$emit("update",this.value + 1)
         }
     }
}
</script>

<style>

</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容