美文网首页
Vue.js 组件通信 v-model

Vue.js 组件通信 v-model

作者: Rinaloving | 来源:发表于2019-08-21 15:47 被阅读0次
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>组件通信:使用v-model</title>
</head>
<body>

    <!--自动识别最新稳定版本的Vue.js-->
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>

    <div id="app">
        <p>总数:{{ total }}</p>
        <my-component v-model="total"></my-component>
    </div>

    <script>
         Vue.component('my-component',{
            template:'<button @click="handleClick">+1</button>',
             data:function(){
                 return {
                     counter:0
                 }
             },
             methods:{
                 handleClick:function(){
                     this.counter++;
                     this.$emit('input',this.counter)
                 }
             }
         });

        var app = new Vue({
            el:'#app',
            data:{
                total:0
            }
        });

    </script>

</body>
</html>

组建通信v-model.png

相关文章

网友评论

      本文标题:Vue.js 组件通信 v-model

      本文链接:https://www.haomeiwen.com/subject/hmqisctx.html