1. <body>
    2. <div id="app">
    3. <!-- 某些结果是基于之前数据实时计算出来的,我们可以利用计算属性。来完成 -->
    4. <ul>
    5. <li>西游记; 价格:{{xyjPrice}},数量:<input type="number" v-model="xyjNum"> </li>
    6. <li>水浒传; 价格:{{shzPrice}},数量:<input type="number" v-model="shzNum"> </li>
    7. <li>总价:{{totalPrice}}</li>
    8. {{msg}}
    9. </ul>
    10. </div>
    11. <script src="../node_modules/vue/dist/vue.js"></script>
    12. <script>
    13. //watch可以让我们监控一个值的变化。从而做出相应的反应。
    14. new Vue({
    15. el: "#app",
    16. data: {
    17. xyjPrice: 99.98,
    18. shzPrice: 98.00,
    19. xyjNum: 1,
    20. shzNum: 1,
    21. msg: ""
    22. },
    23. computed: {
    24. totalPrice(){
    25. return this.xyjPrice*this.xyjNum + this.shzPrice*this.shzNum
    26. }
    27. },
    28. watch: {
    29. xyjNum(newVal,oldVal){
    30. if(newVal>=3){
    31. this.msg = "库存超出限制";
    32. this.xyjNum = 3
    33. }else{
    34. this.msg = "";
    35. }
    36. }
    37. },
    38. })
    39. </script>
    40. </body>