1. <div id="app">
    2. {{total}}
    3. </div>
    4. <script>
    5. var vm = new Vue({
    6. el: "#app",
    7. data() {
    8. return {
    9. firstName: 1,
    10. lastName: 2,
    11. total: 3
    12. }
    13. },
    14. /* 监听data中的值,只要值改变函数就会触发 */
    15. watch: {
    16. firstName() {
    17. this.total = this.firstName + this.lastName
    18. },
    19. lastName() {
    20. this.total = this.firstName + this.lastName
    21. }
    22. }
    23. })
    24. </script>