您的位置:首页 > 产品设计 > UI/UE

vue 用 key 管理可复用的元素

2018-01-10 17:22 330 查看
<!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