我们遇到了类似的问题,有多个页面、一个布局和多个组件。我们的系统不是 SPA。每个页面重新加载。页面内容通过服务器代码插入到具有一些全局选项的全局布局中。
我们有全局组件和一些更具体的页面,我们的解决方案是使用window捕获Vue并在按页面加载组件后初始化vue。
重要提示:遵循此顺序声明:windowVue / 页面特定代码 / startVue
前:
布局文件:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="../js/windowVue.js"></script>
<!-- all header content -->
<!-- depend your system here call your js specific by page ex: productPage.js -->
<script type="text/javascript" src="../js/productPage.js"></script>
</head>
<body>
<div id="vueApp">
<!-- Your page content-->
</div>
<script type="text/javascript" src="../js/startVue.js"></script>
</body>
</html>
windowVue.js
import Vue from 'vue';
window.Vue = Vue;
productPage.js
// Here two options declare the external components only
import ComponentA from '../js/components/component-a.js';
import ComponentB from '../js/components/component-b.vue';
window.Vue.component('component-a', ComponentA)
window.Vue.component('component-b', ComponentB)
// Or if you use a page component with more logic and options you can declare here
// and include the other components as usual
window.Vue.component('product-page', {
components: {
ComponentA,
ComponentB
},
// rest of the code as usual
})
startVue.js
import GlobalA from '../js/components/global-a.js';
import GlobalB from '../js/components/global-B.js';
new window.Vue({
el:"#vueApp",
delimiters: ['${', '}'],
components: {
GlobalA,
GlobalB
}
})
现在每个页面都有自己的组件,我们也有一些共享组件。
一些备注:
- 单独构建3个js部分
- 只有 windowVue.js 使用
import Vue from 'vue',其余使用window.Vue。
- .js 文件组件被声明为对象。
组件-a.js
import ComponentB from '../js/components/component-b.vue';
import ComponentC from '../js/components/component-c.vue';
import ComponentD from '../js/components/component-d.vue';
export default {
name:'component-a',
components: {
ComponentB,
ComponentC,
ComponentD
},
data() {
return {
variableExample: 'example'
}
} // more of your Vue code
}