zoukankan      html  css  js  c++  java
  • 自定义组件的v-model

    <input v-model="searchText">
    

     等价于

    <input
      v-bind:value="searchText"
      v-on:input="searchText = $event.target.value"
    >
    

    下面直接举例

    父组件:

    <template>
    <div>
        <basetest v-model="textvalue">
            <!--插槽可以放任何模板代码,甚至是组件(不能访问子组件数据)-->
            我是default插槽{{msg}}
    
            <!--具名插槽-->
            <template v-slot:head>
                <div>我是head插槽</div>
            </template>
        </basetest>
        {{textvalue}}
    </div>
    </template>
    
    <script>
    import basetest from './basetest';
    export default {
        components:{
            basetest: basetest
        },
        data(){
            return {
                textvalue: '',
                msg: '333'
            }
        }
    }
    </script>

    子组件:

    <template>
        <div>
            <div><input type="text" :value="value" @input="myfunc"></div>
            <label>
                <slot>没有提供内容的时候显示</slot>
            </label>
            <span>
                <slot name="head"></slot>
            </span>
            <button @click="btnFun">取我的值</button>
        </div>
    </template>
    
    <script>
    export default {
        model:{
            prop: 'value',
            event: 'testFunc'
        },
       props: {
           value: ''
       },
       methods:{
           myfunc(e){
               this.$emit('testFunc', e.target.value)
           },
           btnFun(){
               this.$emit('testFunc', 'yyyyyy')
           }
       }
    }
    </script>

    只要触发了testFunc方法,都能改变父组件textvalue的值

  • 相关阅读:
    SIMPLE QUERY几个原则
    [POI2014]DOO-Around the world
    Java 实现 蓝桥杯 历届试题 分糖果
    or小计
    luoguP1357 花园
    like小计
    [NOI2016]区间
    complex query几个原则
    AGC 018E.Sightseeing Plan——网格路径问题观止
    排查一般MySQL性能问题
  • 原文地址:https://www.cnblogs.com/myyouzi/p/13900572.html
Copyright © 2011-2022 走看看