1、v-if决定标签是否显示
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
- <script src="https://cdn.staticfile.net/vue/2.2.2/vue.min.js"></script>
- </head>
- <body>
- <div id="app">
- <p v-if="seen">现在你看到我了</p>
- <template v-if="ok">
- <p>学的不仅是技术,更是梦想!</p>
- <p>哈哈哈,打字辛苦啊!!!</p>
- </template>
- </div>
-
- <script>
- new Vue({
- el: '#app',
- data: {
- seen: true,
- ok: true
- }
- })
- </script>
- </body>
- </html>
2、v-show
v-show指令和v-if指令的作用一样,都是跟后面的表达式真假来决定元素标签是否展示的
为什么明明已经有v-if了,还要创造个v-show出来呢?
答案很简单:他俩的真正实现技术 和 性能效果是有差异的
v-if适合不频繁变化的场景 v-show适合频繁切换真假的场景
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
- <script src="https://cdn.staticfile.net/vue/2.2.2/vue.min.js"></script>
- </head>
- <body>
- <div id="app">
- <h1 v-show="ok">Hello!</h1>
- </div>
-
- <script>
- new Vue({
- el: '#app',
- data: {
- ok: true
- }
- })
- </script>
- </body>
- </html>
3、v-else v-else-if
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
- <script src="https://cdn.staticfile.net/vue/2.2.2/vue.min.js"></script>
- </head>
- <body>
- <div id="app">
- <div v-if="type === 'A'">
- A
- </div>
- <div v-else-if="type === 'B'">
- B
- </div>
- <div v-else-if="type === 'C'">
- C
- </div>
- <div v-else>
- Not A/B/C
- </div>
- </div>
-
- <script>
- new Vue({
- el: '#app',
- data: {
- type: 'C'
- }
- })
- </script>
- </body>
- </html>
4、v-for 循环 循环列表 循环字典
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
- <script src="https://cdn.staticfile.net/vue/2.2.2/vue.min.js"></script>
- </head>
- <body>
- <div id="app">
- <ol>
- <li v-for="site in sites">
- {{ site.name }}
- </li>
- </ol>
-
- <ol>
- <li v-for="site in sites1">
- {{ site }}
- </li>
- </ol>
- </div>
-
- <script>
- new Vue({
- el: '#app',
- data: {
- sites: [
- { name: 'Runoob' },
- { name: 'Google' },
- { name: 'Taobao' }
- ],
- sites1:['zhaocuixia','zhaohongxia','zhaoyuxia']
-
- }
- })
- </script>
- </body>
- </html>