Vue html评论处理

时间:2017-11-22 04:12:52

标签: html vue.js tags comments conditional-comments

我正在使用Vue制作一些html模板,我需要根据下面的代码包含html条件注释。

var productTemplate = new Vue({
    el: '#myApp'
});
<script src="https://unpkg.com/vue@2.5.8/dist/vue.js"></script>

<div id="myApp">
    <div class="some-content">
        This is some content
    </div>

    <!--[if mso]>
      <div>
          this div will only be shown if the condition in the comment is true, in this case the condition is:
          if ( mso (microsoft office) == the html rendering engine) {
            show the html code between the [if mso] and the [endif]
          }
      </div>
    <![endif]-->

    <div class="some-other-content">
        This is some content
    </div>
</div>

但是当我在浏览器中打开我的html页面时,条件注释之间的html代码被完全删除,即使我确实需要它在那里。

如何让Vue在模板视图中包含这些注释?

2 个答案:

答案 0 :(得分:6)

在Vue 2.4.0 +中,如果要在组件模板中保留注释,可以在组件内部将comments选项设置为true

var productTemplate = new Vue({
    comments: true,  // <-- Add this
    el: '#myApp'
});

答案 1 :(得分:0)

Vue会删除HTML评论。

我能想到的一种方法是将你的注释绑定在一个变量中,并通过v-html指令输出变量。

编辑:我在错误的开发环境中测试了它,所以这里是关于Vue.js GitHub问题的链接。 https://github.com/vuejs/vue/issues/6177

&#13;
&#13;
var productTemplate = new Vue({
    el: '#myApp',
    comments: true,
    data: {
      comments: `    <!--[if mso]>
      <div>
          this div will only be shown if the condition in the comment is true, in this case the condition is:
          if ( mso (microsoft office) == the html rendering engine) {
            show the html code between the [if mso] and the [endif]
          }
      </div>
    <![endif]-->`
    }
});
&#13;
<script src="https://unpkg.com/vue@2.5.8/dist/vue.js"></script>

<div id="myApp">
    <div class="some-content">
        This is some content
    </div>
    <!-- Comments -->
    <div v-html="comments"> {{ comments }} </div>

    <div class="some-other-content">
        This is some content
    </div>
</div>
&#13;
&#13;
&#13;