Blogger Information
Blog 46
fans 0
comment 0
visits 39177
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
演示条件渲染,计算属性与侦听器属性
lus菜
Original
640 people have browsed it

条件渲染:v-if,v-show

  1. v-if: 元素是否添加到页面中
  2. v-show: 元素是否显示出来(元素已经在dom元素结构中)

样式代码:

  1. <body>
  2. <div id="app">
  3. <!-- 单分支 -->
  4. <p v-if="flag">{{msg}}</p>
  5. <button @click="flag = !flag" v-text="tips = flag ? `隐藏`: `显示`"></button>
  6. <!-- 多分支 -->
  7. <p v-if="point > 1000 && point < 2000">{{grade[0]}}</p>
  8. <p v-else-if="point >= 2001 && point < 3000">{{grade[1]}}</p>
  9. <p v-else-if="point >= 3001 && point < 4000">{{grade[2]}}</p>
  10. <p v-else-if="point >= 4000 ">{{grade[3]}}</p>
  11. <p v-else>{{grade[4]}}</p>
  12. <!-- v-show -->
  13. <p v-show="status">祝大家牛年吉祥如意</p>
  14. </div>
  15. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  16. <script>
  17. const vm = new Vue({
  18. el: document.querySelector("#app"),
  19. data() {
  20. return {
  21. msg: "明天我请大家吃饭",
  22. flag: true,
  23. tips: "隐藏",
  24. point: 4000,
  25. grade: [
  26. "青铜会员",
  27. "白银会员",
  28. "黄金会员",
  29. "白金会员",
  30. "只对VIP开放",
  31. ],
  32. status: true,
  33. };
  34. },
  35. });
  36. </script>
  37. </body>

效果预览:

计算器属性:

  1. 计算属性,本质上就是原生的访问器属性
  2. 计算属性: 最终会和data合并,所以不要和data中已有属性重名
  3. 访问器属性有getter/setter

样式代码:

  1. <body>
  2. <div class="app">
  3. <p>数量:<input type="number" v-model="num" style="width: 3em" min="0" /></p>
  4. <p>单价: {{price}} 元</p>
  5. <p>金额: {{amount}} 元</p>
  6. </div>
  7. <script>
  8. const vm = new Vue({
  9. el: document.querySelector(".app"),
  10. data() {
  11. return {
  12. num: 1,
  13. price: 50,
  14. res: 0,
  15. };
  16. },
  17. computed: {
  18. //计算属性默认getter
  19. amount: {
  20. get() {
  21. return this.num * this.price;
  22. },
  23. set(value) {
  24. console.log(value);
  25. if (value > 1000) this.price = 80;
  26. },
  27. },
  28. },
  29. });
  30. vm.amount = 1100;
  31. </script>
  32. </body>

效果预览:

侦听器属性:

  1. 侦听器属性:监听数据对象的变化
  2. 侦听器是某一个属性的值的变化,它的属性名与data中要监听的属性同名

样式代码:

  1. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  2. <body>
  3. <div class="app">
  4. <p>
  5. <input
  6. type="number"
  7. v-model="num"
  8. style="width: 3em"
  9. min="0"
  10. max="max"
  11. />
  12. </p>
  13. <p>单价:{{price}} 元</p>
  14. <p>金额: {{res}} 元</p>
  15. </div>
  16. <script>
  17. const vm = new Vue({
  18. el: document.querySelector(".app"),
  19. data() {
  20. return {
  21. num: 0,
  22. price: 50,
  23. res: 0,
  24. max: 100,
  25. };
  26. },
  27. // 侦听器属性
  28. watch: {
  29. num(newValut, oldValue) {
  30. console.log(newValut, oldValue);
  31. // this.res = this.num * this.price;
  32. this.res = newValut * this.price;
  33. // 监听库存
  34. if (newValut >= 20) {
  35. this.max = newValut;
  36. alert("库存不足,请联系管理");
  37. }
  38. },
  39. },
  40. });
  41. </script>
  42. </body>

效果预览:

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