Blogger Information
Blog 45
fans 0
comment 0
visits 34632
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
VUE条件渲染, 计算属性和侦听器
咸鱼老爷
Original
517 people have browsed it

条件渲染

  • v-if:元素知否添加到页面中
    为true时添加,为false时不添加

    1. <p v-if='flag'>{{msg}}</p>
    2. <script>
    3. const vm = new Vue({
    4. el: '#app',
    5. data() {
    6. return {
    7. msg: "晚上好",
    8. flag: true,
    9. }
    10. }
    11. })
    12. </script>

    点击显示与不显示小案例

    1. <p v-if='flag'>{{msg}}</p>
    2. <button @click="flag=!flag" v-text="tips=flag?`隐藏`:`显示`"></button>
    3. <script>
    4. const vm = new Vue({
    5. el: '#app',
    6. data() {
    7. return {
    8. msg: "晚上好",
    9. flag: true,
    10. tips: '隐藏',
    11. }
    12. }
    13. })
    14. </script>

    多分支条件

    v-if v-else-if v-else

    1. <p v-if="score > 100 && score < 2000">{{level[0]}}</p>
    2. <p v-else-if="score >= 2001 && score < 3000">{{level[1]}}</p>
    3. <p v-else-if="score >= 3001 && score < 4000">{{level[2]}}</p>
    4. <p v-else>{{level[3]}}</p>
    5. <script>
    6. const vm = new Vue({
    7. el: '#app',
    8. data() {
    9. return {
    10. msg: "晚上好",
    11. flag: true,
    12. tips: '隐藏',
    13. score: 10000,
    14. level: ["铜牌", "银牌", "金牌", "王牌"],
    15. }
    16. }
    17. })
    18. </script>

    • v-show:元素是否显示出来(元素已经在dom中存在)

      1. <p v-show='status'>这是v-show</p>
      2. <script>
      3. const vm = new Vue({
      4. el: '#app',
      5. data() {
      6. return {
      7. status: true,
      8. }
      9. }
      10. })
      11. </script>

      计算属性

      计算属性,本质上就是原生的访问属性
      计算属性最终会和data合并,所以不要和data中已有属性重名

      1. <div id="app">
      2. <p>数量: <input type="number" v-model='num' min="0"></p>
      3. <p>单价:{{price}}元</p>
      4. <p>金额:{{amout}}元</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. computed: {
      17. amout: {
      18. get() {
      19. return this.num * this.price
      20. },
      21. set(value) {
      22. console.log(value);
      23. if (value > 1000) {
      24. this.price = 40
      25. }
      26. }
      27. }
      28. }
      29. })
      30. vm.amout = 1001;
      31. </script>

      侦听器属性 watch

      侦听的是某一个属性的值的变化,它的属性名与data中要监听的属性同名

      1. <div id="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: 100,
      15. }
      16. },
      17. watch: {
      18. num(newValue, oldValue) {
      19. console.log("new:" + newValue, 'old' + oldValue);
      20. // this.res = this.num * this.price
      21. this.res = newValue * this.price;
      22. //监听库存
      23. if (newValue >= 20) {
      24. this.max = newValue;
      25. alert('库存不足');
      26. }
      27. }
      28. }
      29. })
      30. </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