美文网首页
17 vue-组件 v-model

17 vue-组件 v-model

作者: 滔滔逐浪 | 来源:发表于2021-08-06 22:44 被阅读0次

渲染普通的HTML元素在Vue中是非常快速的,但有的时候你可能有一个组件,这个个组件包含了大量静态的内容,在这种情况下,你可以在跟元素上添加v-once attribute 以确保这些内容只计算一次然后缓存起来,

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="../../lib/vue.js"></script>
        <script src="../../lib/axios.js"></script>
    </head>
    <body>
        <div id="app">
            <kerwin-input type="text" title="姓名" v-model="username"></kerwin-input>
            <kerwin-input type="password" title="密码" v-model="password" ></kerwin-input>
            <button>submit</button>
                <button>seset</button>
        </div>
 <script>
    Vue.component("kerwinInput",{
        data(){
            return{
                mytext:""
            }
        },
        props:["type","title","value"],
        template:`<div>
    <label> {{title}}</label>
{{value}}
          <input :type="type" style="background:red" v-model="mytext"
           @input="handleInput">
        
        
        </div>`,
        methods:{
            handleInput(event){
                console.log(event.target.value);
                this.$emit("input",event.target.value)
            }
        }
        
    });
    var vm=new Vue({
        el:"#app",
        data:{
            username:"",
            password:"",
             isShow:false
            
        },
        methods:{
            event1(data){
                console.log(data)
                this.username=data;
            },
            event2(data){
                this.password=data
                console.log(data)
            }
        }
        
    });
</script>
    </body>
</html>

动态 组件:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="../../lib/vue.js"></script>
        <script src="../../lib/axios.js"></script>
    </head>
    <style type="text/css">
      
        *{
          margin: 0px;
          padding: 0px;
        }
    
        html,body{
          width: 100%;
          height: 100%;
        }
       footer ul {
        display: flex;
        position: fixed;
        left: 0px;
        bottom: 0px;
        width: 100%;
        height: 40px;
       }
    
       footer ul li {
        flex: 1;
        text-align: center;
        list-style: none;
        height: 40px;
        line-height: 40px;
        background: gray;
       }
    </style>
    <body>
        <div id="app">
            <keep-alive>
            <comment :is="isWhich"></comment>
            </keep-alive>
            <!-- <home v-show="isWhich===1"></home>
            <list  v-show="isWhich===2"></list>
            <shopcar  v-show="isWhich===3"></shopcar> -->
            <footer>
               <ul>
                   <li @click="isWhich='home'">首页</li>
                     <li  @click="isWhich='list'">列表</li>  
                     <li  @click="isWhich='shopcar'"> 购物车</li>
                   
               </ul></footer>
        </div>

<script>
    
    Vue.component("home",{
        template:`
        <div>home
        <input type="text">
        </div>
        `
    })
    Vue.component("list",{
        template:`
        <div>list</div> 
        `
    })
    
    Vue.component("shopcar",{
        template:`
        <div>shopcar</div>
        `
    })
    new Vue({
        el:"#app",
        data:{
            isWhich:"home"
            
        }
        
    });
</script>
    </body>
</html>


相关文章

网友评论

      本文标题:17 vue-组件 v-model

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