“Vue v-if 指令”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
| 第1行: | 第1行: | ||
https://www.bilibili.com/video/BV12J411m7MG/?p=12 | https://www.bilibili.com/video/BV12J411m7MG/?p=12 | ||
| − | 根据表达式的真假,切换元素的显示和隐藏(操纵 dom 元素) | + | 根据表达式的真假,切换元素的显示和隐藏(操纵 dom 元素)<syntaxhighlight lang="html"> |
| + | <div> | ||
| + | <p v:if="true">我是一个p标签</p> | ||
| + | <p v:if="isShow">我是一个p标签</p> | ||
| + | <p v:if="表达式">我是一个p标签</p> | ||
| + | </div> | ||
| + | |||
| + | var app = new Vue({ | ||
| + | el:"#app", | ||
| + | data:{ | ||
| + | isShow:false | ||
| + | } | ||
| + | }) | ||
| + | </syntaxhighlight><syntaxhighlight lang="html"> | ||
| + | <!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>v-if指令</title> | ||
| + | </head> | ||
| + | |||
| + | <body> | ||
| + | <div id="app"> | ||
| + | <input type="button" value="切换显示" @click="toggleIsShow"> | ||
| + | <p v-if="isShow">hello world</p> | ||
| + | <p v-show="isShow">hello world from v-show</p> | ||
| + | <h2 v-if="temperature>=35">高温警报</h2> | ||
| + | </div> | ||
| + | <!-- 开发环境版本,包含了有帮助的命令行警告 --> | ||
| + | <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> | ||
| + | <script> | ||
| + | var app = new Vue({ | ||
| + | el: "#app", | ||
| + | data: { | ||
| + | isShow: false, | ||
| + | temperature:20 | ||
| + | }, | ||
| + | methods: { | ||
| + | toggleIsShow: function () { | ||
| + | this.isShow = !this.isShow; | ||
| + | } | ||
| + | } | ||
| + | }) | ||
| + | </script> | ||
| + | </body> | ||
| + | </html> | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === 总结 === | ||
| + | |||
| + | * <u>v-if</u> 指令的作用是:根据表达式的真假切换元素的显示状态 | ||
| + | * 本质是通过操纵 <u>dom</u> 元素来切换显示状态 | ||
| + | * 表达式的值为 <u>true</u>,元素存在于 <u>dom</u> 树中,为 <u>false</u>,从 <u>dom</u> 树中移除 | ||
2023年6月9日 (五) 03:11的最新版本
https://www.bilibili.com/video/BV12J411m7MG/?p=12
根据表达式的真假,切换元素的显示和隐藏(操纵 dom 元素)
<div>
<p v:if="true">我是一个p标签</p>
<p v:if="isShow">我是一个p标签</p>
<p v:if="表达式">我是一个p标签</p>
</div>
var app = new Vue({
el:"#app",
data:{
isShow:false
}
})
<!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>v-if指令</title>
</head>
<body>
<div id="app">
<input type="button" value="切换显示" @click="toggleIsShow">
<p v-if="isShow">hello world</p>
<p v-show="isShow">hello world from v-show</p>
<h2 v-if="temperature>=35">高温警报</h2>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
var app = new Vue({
el: "#app",
data: {
isShow: false,
temperature:20
},
methods: {
toggleIsShow: function () {
this.isShow = !this.isShow;
}
}
})
</script>
</body>
</html>
总结
- v-if 指令的作用是:根据表达式的真假切换元素的显示状态
- 本质是通过操纵 dom 元素来切换显示状态
- 表达式的值为 true,元素存在于 dom 树中,为 false,从 dom 树中移除