Blogger Information
Blog 41
fans 0
comment 0
visits 31057
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue的条件渲染、计算属性和侦听器属性
陈强
Original
541 people have browsed it

key的作用

key的作用主要是为了高效的更新虚拟DOM,此处的key就好比数据库里面主键的概念,通过它可以唯一的确定一组节点。

  • v-for 指令必须搭配:key
  1. <li v-for="item of list" :key="item.id">

条件渲染

  • v-if
  1. //v-if单分支,值为布尔类型,true \ false
  2. <li v-if="status">{{message}}</li>
  3. //多分支 v-if v-else-if
  4. <li v-if="num >= 0 && num < 2">{{message[0]}}</li>
  5. <li v-else-if="num > 2 && num < 10">{{message[1]}}</li>
  6. <li v-else-if="num > 11 && num < 20">{{message[2]}}</li>
  7. <li v-else>{{message[3]}}</li>
  8. <script>
  9. const vm = new Vue({
  10. el: ".app",
  11. data() {
  12. return {
  13. status: 8,
  14. message: ["susses", "error", "404", "lose"],
  15. };
  16. },
  17. });
  18. </script>
  • v-show
  1. <p v-show="num">{{message[3]}}</p>
  2. num: true,
  3. message: ["susses", "error", "404", "lose"],

计算属性

  1. <div class="app">
  2. <p>数量:<input type="number" v-model="num" min="0" /></p>
  3. <p>单价:{{price}}</p>
  4. <p>金额:{{amount}}</p>
  5. </div>
  6. <script>
  7. const vm = new Vue({
  8. el: ".app",
  9. data() {
  10. return {
  11. num: 0,
  12. price: 50,
  13. res: 0,
  14. };
  15. },
  16. //计算属性用conputed声明
  17. computed: {
  18. amount() {
  19. //如果数量大于10,单价变成40
  20. if (this.num >= 10) this.price = 40;
  21. return (this.res = this.num * this.price);
  22. },
  23. },
  24. });
  25. </script>

侦听器属性

  1. <div class="app">
  2. <p>数量:<input type="number" v-model="num" min="0" :max="max" /></p>
  3. <p>单价:{{price}}</p>
  4. <p>金额:{{res}}</p>
  5. </div>
  6. <script>
  7. const vm = new Vue({
  8. el: ".app",
  9. data() {
  10. return {
  11. num: 0,
  12. price: 50,
  13. res: 0,
  14. max: 20,
  15. };
  16. },
  17. watch: {
  18. num(newValut, oldValue) {
  19. this.res = newValut * this.price;
  20. if (newValut >= 5) {
  21. this.max = newValut;
  22. alert("本商品最大购买数量为" + newValut);
  23. }
  24. },
  25. },
  26. });
  27. </script>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post