05_姓名案例_监视属性实现.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <link rel="icon" href="../../logo.svg" />
  8. <title>姓名案例_监视属性实现</title>
  9. </head>
  10. <body>
  11. <div id="root">
  12. <div>姓:<input type="text" v-model="firstName" /></div>
  13. <div>名:<input type="text" v-model="lastName" /></div>
  14. <div>全名:{{fullName}}</div>
  15. </div>
  16. <script src="../../vue.js"></script>
  17. <script>
  18. Vue.config.productionTip = false; // 阻止 vue 在启动时生成生产提示。
  19. new Vue({
  20. el: "#root",
  21. data: {
  22. firstName: "张",
  23. lastName: "三",
  24. fullName: "张-三",
  25. },
  26. watch: {
  27. firstName(currentValue) {
  28. this.fullName = `${currentValue} - ${this.lastName}`;
  29. },
  30. lastName(currentValue) {
  31. this.fullName = `${this.firstName} - ${currentValue}`;
  32. },
  33. },
  34. });
  35. </script>
  36. </body>
  37. </html>