纵有疾风起
人生不言弃

vue 用 key 管理可复用的元素

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/sunling112/article/details/79026264

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue学习</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>

<body>
    <div id="app1">
        <template>
            <div v-if="loginType === 'username'">
                <label>username</label>
                <input type="text" placeholder="enter your username" name="abc" key="eee">           
                <!--vue是高效复用的,input还是2个input,当时输入缓存还在,为了区别开,用关键字key-->
            </div>
            <div v-else>
                <label>passworld</label>
                <input type="password" placeholder="enter your password" name="dd" key="yyy">         <!--key 在浏览器控制台不显示哦,但能完美解决问题-->
            </div>
        </template>
        <button v-on:click="fun">点击切换</button>  

    </div>
    <script>
        var foo = new Vue({
            el: '#app1',
            data: {
                loginType: 'username',
            },
            methods:{
                fun:function(){
                    this.loginType =='username' ? this.loginType='password' : this.loginType='username';
                }
            }

        });
    </script>
</body>

</html>

未经允许不得转载:起风网 » vue 用 key 管理可复用的元素
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录