编辑
2024-11-01
编程
00

目录

一、Vue3条件渲染
1. 条件渲染的基本使用
2. 多个条件渲染
3temolate和v-if一起使用
4. v-show和v-if的区别

一、Vue3条件渲染

1. 条件渲染的基本使用

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>条件渲染的基本使用</title> </head> <body> <div id="app"></div> <template id="my-app"> <h2 v-if="isShow">{{message}}</h2> <button @click="btnCleck">切换</button> </template> <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.prod.js"></script> <script> const App = { template: '#my-app', data() { return { message: "Hello World", isShow: true } }, methods: { btnCleck() { this.isShow = !this.isShow; } } } const app = Vue.createApp(App).mount("#app"); </script> </body> </html>

2. 多个条件渲染

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>多个条件渲染</title> </head> <body> <div id="app"></div> <template id="my-app"> <input type="text" v-model="score"> <h2 v-if="score > 90">优秀</h2> <h2 v-else-if="score > 60">及格</h2> <h2 v-else>不及格</h2> </template> <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.prod.js"></script> <script> const App = { template: '#my-app', data() { return { message: "Hello World", score: 95 } }, methods: { } } const app = Vue.createApp(App).mount("#app"); </script> </body> </html>

3temolate和v-if一起使用

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>temolate和v-if一起使用</title> </head> <body> <div id="app"></div> <template id="my-app"> <template v-if="isShow"> <h2>test1</h2> <h2>test1</h2> <h2>test1</h2> </template> <template v-else> <h2>test2</h2> <h2>test2</h2> <h2>test2</h2> </template> </template> <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.prod.js"></script> <script> const App = { template: '#my-app', data() { return { message: "Hello World", isShow: true } }, methods: { } } const app = Vue.createApp(App).mount("#app"); </script> </body> </html>

4. v-show和v-if的区别

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>v-show和v-if的区别</title> </head> <body> <div id="app"></div> <template id="my-app"> <!-- v-show不支持template --> <h2 v-if="isShow">test1</h2> <!-- v-show不可以和v-else一起使用 --> <h2 v-show="isShow">test2</h2> </template> <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.prod.js"></script> <script> const App = { template: '#my-app', data() { return { message: "Hello World", isShow: true } }, methods: { } } const app = Vue.createApp(App).mount("#app"); </script> </body> </html>

本文作者:ruiwiki

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!