template 模板属性

说明

image.png

写法

核心都是构建虚拟的DOM节点,然后替换正在显示的DOM元素

写法1、x-template

这个时候,在createApp的对象中,我们需要传入的template以 # 开头,
如果字符串是以 # 开始,那么它将被用作 querySelector,并且使用匹配元素的 innerHTML 作为模板字符串

  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. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">哈哈哈哈啊</div>
  11. <script type="x-template" id="why">
  12. <div>
  13. <h2>{{message}}</h2>
  14. <h2>{{counter}}</h2>
  15. <button @click='increment'>+1</button>
  16. <button @click='decrement'>-1</button>
  17. </div>
  18. </script>
  19. <script src="../js/vue.js"></script>
  20. <script>
  21. Vue.createApp({
  22. template: '#why',
  23. data: function() {
  24. return {
  25. message: "Hello World",
  26. counter: 100
  27. }
  28. },
  29. // 定义各种各样的方法
  30. methods: {
  31. increment() {
  32. console.log("点击了+1");
  33. this.counter++;
  34. },
  35. decrement() {
  36. console.log("点击了-1");
  37. this.counter--;
  38. }
  39. }
  40. }).mount('#app');
  41. </script>
  42. </body>
  43. </html>

写法2、任意标签设置id

通常使用template标签,因为不会被浏览器渲染。

template元素是一种用于保存客户端内容的机制,该内容再加载页面时不会被呈现,但随后可以在运行时使用JavaScript实例化;

  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. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. <template id="why">
  12. <div>
  13. <h2>{{message}}</h2>
  14. <h2>{{counter}}</h2>
  15. <button @click='increment'>+1</button>
  16. <button @click='decrement'>-1</button>
  17. <button @click="btnClick">按钮</button>
  18. </div>
  19. </template>
  20. <script src="../js/vue.js"></script>
  21. <script>
  22. document.querySelector("#why")
  23. Vue.createApp({
  24. template: '#why',
  25. data: function() {
  26. return {
  27. message: "Hello World",
  28. counter: 100
  29. }
  30. },
  31. // 定义各种各样的方法
  32. methods: {
  33. increment() {
  34. console.log("点击了+1");
  35. this.counter++;
  36. },
  37. decrement() {
  38. console.log("点击了-1");
  39. this.counter--;
  40. },
  41. btnClick: () => {
  42. // this === window? 不可以
  43. // 写成一个箭头函数时, 这个this就是window
  44. // 在箭头函数中是不绑定this, 但是函数中如果使用了this
  45. console.log(this);
  46. },
  47. btn2Click: function() {
  48. // this === window? 不可以
  49. // 写成一个箭头函数时, 这个this就是window
  50. // 在箭头函数中是不绑定this, 但是函数中如果使用了this
  51. console.log(this);
  52. }
  53. }
  54. }).mount('#app');
  55. </script>
  56. </body>
  57. </html>

==========================

data 数据属性

image.png

==========================

methods 方法属性

image.png
image.png

=========================

其他属性

props、computed、watch、emits、setup 等

=========================

setup Vue3函数编程属性

参考 https://www.yuque.com/yejielin/mypn47/uk9tee#LrQ3k

可以把data、methods等属性都一并使用,有助于分开逻辑和变量