Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue教程05:v-pre、v-cloak指令 #6

Open
chencl1986 opened this issue Mar 18, 2019 · 0 comments
Open

Vue教程05:v-pre、v-cloak指令 #6

chencl1986 opened this issue Mar 18, 2019 · 0 comments

Comments

@chencl1986
Copy link
Owner

阅读更多系列文章请访问我的GitHub博客,示例代码请访问这里

v-pre指令

代码示例:/lesson05/01. v-pre指令.html

v-pre可以用来阻止预编译,有v-pre指令的标签内部的内容不会被编译,会原样输出。

如果已知页面内部有大段内容无需编译,使用v-pre指令阻止编译可以提高性能,同时可以防止页面内有可能导致Vue编译出错的代码存在。

JavaScript:

let vm = new Vue({
  el: '#app', // 根元素或挂载点。
  data: {}
})

HTML:

<div id="app">
  <!-- 若不加v-pre指令,直接编译会报错,因为data中没有a和b属性 -->
  <div v-pre>
    {{a}} + {{b}}
  </div>
</div>

v-cloak指令

代码示例:/lesson05/02. v-cloak指令.html

v-cloak指令只是在标签中加入一个v-cloak自定义属性,在HTML还编译完成之后该属性会被删除,可以CSS对标签设置样式,表示HTML还未被编译,比如可以设置display: none;

JavaScript:

// 延迟3秒实例化Vue,若不加v-cloak指令,在页面上会显示{{a}} + {{b}},1秒之后才渲染出10 + 20。
setTimeout(() => {
  let vm = new Vue({
    el: '#app', // 根元素或挂载点。
    data: {
      a: 10,
      b: 20
    }
  })
}, 3000);

HTML:

<div id="app">
  <!-- v-cloak指令只是在标签中加入一个v-cloak自定义属性,在HTML还编译完成之后该属性会被删除,可以CSS对标签设置样式,表示HTML还未被编译,比如可以设置display: none; -->
  <div v-cloak>
    {{a}} + {{b}}
  </div>
</div>

CSS:

<style>
  /* 有v-cloak属性的标签都不显示 */
  [v-cloak] {
    display: none;
  }
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant