学习优先度:★★★☆☆

难易度:★★★★☆

Vue.js 中的 v-pre 指令用于跳过一个元素及其所有子元素的编译。

下面介绍如何在 Vue.js 模板中使用 v-pre 指令。

【代码示例】

<div id="app">
  <div v-pre>
  {{ This will not be compiled }}
 </div>
  <div>
  {{ This will be compiled: message }}
 </div>
</div>
<script>
 new Vue({
  el: '#app',
  data: {
   message: 'Hello Vue!'
  }
 });
</script>

【执行结果】

<div id="app">
  <div>
  {{ This will not be compiled }}
 </div>
  <div>
  Hello Vue!
 </div>
</div>

v-pre 指令在以下情况下非常有用:您想显示原始 mustache 标记,或者您有大量静态内容,并想通过跳过 DOM 中该部分的编译来优化性能。如果您有大量静态内容,或想将 Vue 模板语法用于 Vue 模板以外的其他内容,这样做可以提高性能。

【补充】

Vue.js 模板会被解析并编译成呈现函数。通过使用 v-pre,你可以告诉 Vue 忽略模板的一部分,这在某些情况下有利于优化呈现性能。