Vue.js常用指令总结

迈不过友情╰ 2023-07-09 05:27 77阅读 0赞

Vue.js的指令作用于HTML元素,以v-开头,例如v-modelv-bind等将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,可以将指令看作特殊的HTML属性(attribute)。

v-text


渲染一个数据,可以与下面v-html的对比理解

v-html


渲染带html标签的数据

  1. <div id="app">
  2. <p v-cloak>{
  3. { msg }}</p>
  4. <p v-text="msg"></p>
  5. <p v-html="msg"></p>
  6. </div>
  7. <script type="text/javascript"> new Vue({ el : "#app", data : { msg : "<h1>这是一个h1元素内容</h1>" } }); </script>

在这里插入图片描述

v-once


渲染一次,之后不再变化

v-bind


绑定html标签属性

  1. <a v-bind:href="url">Google</a>
  2. <!-- 也可以省略冒号前的部分 -->
  3. <a :href="url">Google</a>
  4. <script> new Vue({ el: "#app" data: { url: 'https://google.com' } }) </script>

也可以借助Vue对象间接绑定

  1. <a :[attribute]="google">Google</a>
  2. <!-- 绑定属性 -->
  3. <a v-bind="{ href: google, id: number }">Google</a>
  4. <!-- 通过对象间接绑定属性 -->
  5. <a v-bind="googleObject">Google</a>
  6. <script> new Vue({ el: "#app" data: { google: 'https://google.com' baidu: 'https://baidu.com' number: 1 attribute: 'href' googleObject: { href: 'https://google.com', id: 1 } } }) </script>

v-on


让Vue感知Dom的事件

  1. <p v-on:click="countUp">{
  2. { number }}次点击</p>
  3. <!-- 使用@的省略写法 --->
  4. <p @click="countUp">{
  5. { number }}次点击</p>
  6. <!-- 可以动态指定事件类型 --->
  7. <p @[event]="countUp">{
  8. { number }}次点击</p>
  9. <script> new Vue({ el: "#app" data: { number: 0, event: 'click' } }, methods: { countUp: function() { this.number += 1 } } }) </script>

对于常见事件,Vue中提供了事件修饰符,处理常见逻辑

  1. <!-- prevent用来拦截跳转 -->
  2. <a v-on:click.prevent href="https://google"></a>

对于一些按键事件,Vue也提供了按键修饰符

  1. <!-- 按下enter键,myFunc被调用 -->
  2. <input type="text" v-on:keyup.enter="myFunc" />

v-model


Dom与Vue的双向绑定,model发生更新时实时更新UI

  1. <div id="app">
  2. <input type="text" v-model="message" />
  3. <h1>{
  4. { message }}</h1>
  5. </div>
  6. <script> new Vue({ el: "#app", data: { message: 'Hello World!' } }) </script>

v-if


满足条件时,进行渲染

  1. <div id="app">
  2. <h1 v-if="visible">Text</h1>
  3. <!--template标签也可用-->
  4. <template v-if="visible">
  5. <h1>Text1</h1>
  6. <h1>Text2</h1>
  7. <h1>Text3</h1>
  8. </template>
  9. <button @click="visible = !visible">
  10. {
  11. { visible ? "显示" : "隐藏"}}
  12. </button>
  13. </div>
  14. <script> new Vue({ el: "#app", data: { visible: true } }) </script>

v-show


通过控制样式 display:none来控制元素的显隐。
在显隐逻辑控制上比起v-if性能更高,适合大量地、频繁变动的场景使用

  1. <div id="app">
  2. <h1 v-show="visible">Text</h1>
  3. <button @click="visible = !visible">
  4. {
  5. { visible ? "显示" : "隐藏"}}
  6. </button>
  7. </div>
  8. <script> new Vue({ el: "#app", data: { visible: true } }) </script>

v-else


不满足条件时渲染,必须用跟在v-if后使用

  1. <div id="app">
  2. <p v-if="visible">OK!</p>
  3. <p v-else="visible">Not OK...</p>
  4. <button @click="visible = !visible">
  5. 翻转
  6. </button>
  7. </div>
  8. <script> new Vue({ el: "#app", data: { visible: true } }) </script>

v-else-if


也是必须跟在v-if之后使用

  1. <div id="app">
  2. <p v-if="count == 0">Count = 0</p>
  3. <p v-else-if="count >= 1 && count <= 5">1 ≤ Count ≤ 5</p>
  4. <p v-else> Conut > 6 </p>
  5. <button @click="count += 1">
  6. +1
  7. </button>
  8. </div>
  9. <script> new Vue({ el: "#app", data: { count: 0 } }) </script>

v-for


循环执行渲染,可以取到当前index

遍历数组:

  1. <div id="app">
  2. <ul>
  3. <li v-for="fruit in fruits">{
  4. {fruit}}</li>
  5. </ul>
  6. <ul>
  7. <li v-for="(fruit, index) in fruits">{
  8. {index}}{
  9. {fruit}}</li>
  10. </ul>
  11. <ul>
  12. <template v-for="fruit in fruits">
  13. <li>{
  14. {fruit}}</li>
  15. <hr>
  16. </template>
  17. </ul>
  18. </div>
  19. <script> new Vue({ el: "#app", data: { fruits: ['苹果', '香蕉', '葡萄'] } }) </script>

遍历对象:

  1. <div id="app">
  2. <ul>
  3. <li v-for="value in object">{
  4. {value}}</li>
  5. </ul>
  6. <ul>
  7. <li v-for="(value, key, index) in object">{
  8. {index}}{
  9. {key}}{
  10. {value}}</li>
  11. </ul>
  12. </div>
  13. <script> new Vue({ el: "#app", data: { object: { firstName: 'Steve', lastName: 'Jobs', age: 20 } } }) </script>

v-for中尽量带上key,有助于提高性能。vue在渲染时通过key可以最大限度复用dom避免重建

  1. <div id="app">
  2. <ul>
  3. <div v-for="fruit in fruits" :key="fruit">
  4. <p>{
  5. {fruit}}</p>
  6. <input />
  7. </div>
  8. </ul>
  9. <button @click="remove">删除头部元素</button>
  10. </div>
  11. <script> new Vue({ el: "#app", data: { fruits: ['苹果', '香蕉', '葡萄'] }, methods: { remove: function () { this.fruits.shift() } } }) </script>

发表评论

表情:
评论列表 (有 0 条评论,77人围观)

还没有评论,来说两句吧...

相关阅读