// data & methods & computed & watcher
    // computed 和 method 都能实现的一个功能,建议使用 computed,因为有缓存
    // computed 和 watcher 都能实现的功能,建议使用 computed 因为更加简洁

    1. const app = Vue.createApp({
    2. data() {
    3. return {
    4. message: "hello world",
    5. count: 2,
    6. price: 5,
    7. newTotal: 10,
    8. }
    9. },
    10. watch: {
    11. // price 发生变化时,函数会执行
    12. price(current, prev) {
    13. this.newTotal = current * this.count;
    14. }
    15. },
    16. computed: {
    17. // 当计算属性依赖的内容发生变更时,才会重新执行计算
    18. total() {
    19. return Date.now() + this.count;
    20. // return this.count * this.price
    21. }
    22. },
    23. methods: {
    24. formatString(string) {
    25. return string.toUpperCase();
    26. },
    27. // 只要页面重新渲染,才会重新计算
    28. getTotal() {
    29. return Date.now();
    30. // return this.count * this.price;
    31. },
    32. },
    33. template: `
    34. <div> {{message}} {{newTotal}} </div>
    35. `
    36. });
    37. const vm = app.mount('#root');