javascript - 将 Vue.js 混合到现有的 SSR 网站中?

标签 javascript vue.js progressive-enhancement

是否可以/可行地使用 Vue 来创建实例化到由例如呈现的自定义标签上的组件? PHP 应用程序?某种“自定义元素灯”?

如果我将 Vue 实例挂载到页面根元素上,它“有效”,但据我所知,Vue 在这种情况下使用整个页面作为模板。我想这可能是一个性能问题,如果其他 javascript 代码与 DOM 混淆会导致麻烦。

目标是逐步替换遗留的 jQuery 代码。这个问题有通用的方法吗?

编辑:嵌套这些组件也是一项要求。一个简单的例子是嵌套的可折叠物。

编辑 2:一些 fiddle 来说明问题。

基于 riot.js 的可行解决方案: https://jsfiddle.net/36xju9sh/

<div id="page">
  <!-- This is supposed to show "This is a test." twice. -->
  <test>
    <test></test>
  </test>
</div>
<script type="riot/tag">
  <test>
    <p>This is a test.</p>
    <yield/>
  </test>
</script>
<script>riot.mount('*')</script>

使用 Vue.js 的两种方法: https://jsfiddle.net/89ejjjsy/1/

HTML:

<div id="page">
  <!-- This is supposed to show "This is a test." twice. -->
  <test>
    <test></test>
  </test>
</div>
<script type="text/x-template" id="test-template">
  <p>This is a test.</p>
  <slot></slot>
</script>

Javascript:

Vue.config.ignoreCustomElements = ['test'];

// This won't hit the nested <test> element.
new Vue({
    el:'test',
    template: '#test-template',
});

// This does, but takes over the whole page.
Vue.component('test', {
    template: '#test-template',
});

new Vue({
  el: '#page',
});

最佳答案

您不必使用整个页面作为 Vue 实例范围。例如,使用 #comments-container 作为将嵌套在页面某处的评论组件的范围是非常好的。

您可以在一个页面上有多个 VueJS 实例。

关于javascript - 将 Vue.js 混合到现有的 SSR 网站中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41605325/

相关文章:

javascript - 我如何逐步增强国家和州选择字段?

javascript - 如何检测当前程序运行js中页面的首次加载

javascript - JSON 键名中哪些字符有效/无效?

javascript goto标签如何使用内部循环

javascript - Vue如何正确遍历列表?

javascript - 停止使用 v-model 进行多字段填充?

javascript - 将我的 alfresco javascript 文件转换为 java 类

javascript - VueJS 中的 Handsontable,表格仅在页面重新加载时加载

jquery - 使 jQuery AJAX 具有非 javascript(非 ajax)后备?

javascript - 使用 KnockoutJS 进行渐进式增强