// v-if会频繁的删除DOM并添加DOM
// v-show只是设置display属性
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<div v-if="show" data-test="v-if">{{message}}</div>
<div v-else>bye world</div>
<div v-show="show" data-test="v-show">{{message}}</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
show: false,
message: "hello world"
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<div v-if="show==='a'">This is A</div>
<div v-else-if="show==='b'">This is B</div>
<div v-else>This is others</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
show: 'a'
}
})
</script>
</body>
</html>
// 加key值可以防止input框被多次复用
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<div v-if="show">
用户名:<input key="username"/>
</div>
<div v-else>
邮箱名:<input key="password"/>
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
show: false
}
})
</script>
</body>
</html>
网友评论