【问题标题】:How update the vue template at the run-time?如何在运行时更新 vue 模板?
【发布时间】:2021-02-02 06:20:11
【问题描述】:

我正在开发基于 CMS 的 vue 页面。在页面中,我有一个根容器,里面有两个子容器,如下所示

<div id="app">
    <div class="above-the-fold">...</div>
    <div class="below-the-fold noscript-container">
        <noscript>
            <div v-if="true">{{someDynamicBinding}}</div>
            <div v-if="false">{{someDynamicBinding}}</div>
        </noscript>
    </div>
</div>

如果启用了 javascript,我将删除 noscript 标记并使用以下脚本将内部内容附加到父元素。

document.addEventListener("DOMContentLoaded", function () {
  const appendContents = function () {
    const noScript = document.querySelector(".noscriptcontainer noscript");
    const belowContent = noScript.innerHTML;
    noScript.parentElement.innerHTML += belowContent;
    console.log("elm appended");
    window.removeEventListener("scroll", appendContents);
    noScript.remove();
    console.log("eve removed");
  };
  window.addEventListener("scroll", appendContents);
});

现在的问题是 vue 没有评估 v-if{{dynamicBinding}}
jsfiddle链接https://jsfiddle.net/69yr3mp5/2/

现在完成这项工作的最佳方法是什么?

【问题讨论】:

    标签: javascript typescript vue.js vuejs2 vuejs3


    【解决方案1】:

    尝试用 v-else-if 替换第二个 if

    
                    <div v-if="true"><h1>
                            {{someDynamicBinding}}
                        </h1></div>
                        <div v-else-if="false"><h1>
                            {{someDynamicBinding}}
                        </h1></div>
                    </noscript>
    
    

    【讨论】:

    • 这没有任何意义。它仍然没有在折叠容器下方重新编译代码
    【解决方案2】:

    TL;DR;

    您可以更新模板运行时,但您无法在组件挂载后更新模板


    在这些框架的上下文中,运行时意味着它不是预编译的,这是 Vue 支持的。但是,这里的问题(其中之一)是您在安装应用程序后修改模板。 Vue 应用程序在初始读取后不会继续检查模板。虽然我不确定您要在这里完成什么,但方法似乎是错误的。

    为了说明,如果你使用这段代码,它会编译更新的代码,但直到你滚动。

    let mounted = false
    
    document.addEventListener("DOMContentLoaded", function() {
    
      const appendContents = function() {
        const noScript = document.querySelector(".noscript-container noscript");
        const belowContent = noScript.innerHTML;
        noScript.parentElement.innerHTML += belowContent;
        console.log("elm appended");
        window.removeEventListener("scroll", appendContents);
        noScript.remove();
        console.log("eve removed");
    
        if (!mounted) {
          Vue.createApp({
            data() {
              return {
                someDynamicBinding: 'Some Content'
              };
            },
            mounted() {
              console.log('mounted')
            },
          }).mount('#app');
        };
        mounted = true
      }
    
      window.addEventListener("scroll", appendContents);
    });
    

    【讨论】:

    • 这里我不想在折叠内容下方显示,直到用户在页面上进行交互。 noscript 标记内的内容不会在页面加载时呈现内容。所以它会提高我们的页面加载速度。这就是我们尝试这种方法的原因。
    • 这不是这样做的方法。查看代码拆分,这里有一篇文章更详细地描述了它vueschool.io/articles/vuejs-tutorials/…
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 2015-07-12
    • 2013-01-04
    • 1970-01-01
    • 2012-11-13
    • 2011-11-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多