只渲染元素和组件一次。

    随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。

    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="my-app">
    12. <!-- 这里的会变化 -->
    13. <h2>{{counter}}</h2>
    14. <!-- 这里的子元素只渲染1次,意味着不会变化 -->
    15. <div v-once>
    16. <h2>{{counter}}</h2>
    17. <h2>{{message}}</h2>
    18. </div>
    19. <button @click="increment">+1</button>
    20. </template>
    21. <script src="https://unpkg.com/vue@next"></script>
    22. <script>
    23. const App = {
    24. template: '#my-app',
    25. data() {
    26. return {
    27. counter: 100,
    28. message: "abc"
    29. }
    30. },
    31. methods: {
    32. // 让counter+1
    33. increment() {
    34. this.counter++;
    35. }
    36. }
    37. }
    38. Vue.createApp(App).mount('#app');
    39. </script>
    40. </body>
    41. </html>

    image.png