我想创建一个基本的Vue应用程序,提供登录、侧栏导航等基本功能.但是导航栏项目必须是可互换的.我想创建代表这些导航栏项目的单独Vue应用程序.


Basic idea:

enter image description here


Base app setup

所以我的基础应用程序为这些定制应用程序提供了一条路径

{
  path: '/app/*',
  component: CustomAppContainer,
},

并将呈现这个视图

<template>
  <div id="customAppContainer">Render custom apps here</div>
</template>

我的导航栏可以提供链接/app/one来导航和呈现customAppContainer中名为"one"的应用程序.


Custom app setup

定义路由时,我必须设置base个url

export default new Router({
  base: '/one/',
  routes: [
    {
      path: '/',
      component: Home,
    },
  ],
});

但对于main.js,我不知道如何设置.我想我得更新一下

new Vue({
  router,
  render: h => h(CustomAppContainer),
}).$mount('#customAppContainer');

但显然这个应用程序不知道CustomAppContainer#customAppContainer.


此外,当我只有大约index.html个文件时,我正在考虑发行版,并希望将一个文件集成到另一个文件中.那么,我如何部署一个基本应用程序,当用户希望通过/app/myCustomApp访问自定义应用程序时,我如何将自定义应用程序装载到customAppContainer中?

类似的例子是Nextcloud

开发新的Vue应用程序时,我可以将其作为独立应用程序运行.但对于生产,我想从dist文件夹中获取文件,并将其放入apps目录中,当运行我的基本容器应用程序时,它会在启动时了解所有这些子应用程序.

如果你需要更多信息,请告诉我!

推荐答案

我终于用下面的方法实现了,也许这会对某人有所帮助.Suggestions highly appreciated!


Base App:

呈现所有子应用的基础应用有一个CustomAppContainer视图,该视图将子应用加载到一个div容器中.

<template>
  <div id="customAppContainer"></div>
</template>

<script>
export default {
  mounted() {
    this.updateCustomAppContainer();
  },
  methods: {
    updateCustomAppContainer() {
      const fullRoute = this.$router.currentRoute.fullPath;
      const routeSegments = fullRoute.split("/");
      const appsIndex = routeSegments.indexOf("apps");
      const appKey = routeSegments[appsIndex + 1];

      document.getElementById(
        "customAppContainer"
      ).innerHTML = `<object style="width: 100%; height:100%;" data="http://localhost:3000/subApps/${appKey}"></object>`;
    }
  },
  watch: {
    $route(to, from) {
      this.updateCustomAppContainer();
    }
  }
};
</script>

此外,我在router的基础上增加了mode: 'history'.

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Home
    },
    {
      path: '/apps/*',
      component: CustomAppContainer,
    }
  ]
})

我将该应用程序的构建文件放入名为base的文件夹中的生产目录


File Server:

我创建了一个静态Express文件服务器,它总是为基本应用程序的index.html个文件提供服务.但我也可以申请sub app的index.html文件.

const express = require('express');
const path = require('path');
const fs = require('fs');
const cors = require('cors');
const app = express();
const router = express.Router();

app.use(cors());

app.use(express.static(path.resolve(__dirname, '../base')));

fs.readdirSync(path.resolve(__dirname, '../apps')).forEach(appName => {
    app.use(express.static(path.resolve(__dirname, `../apps/${appName}`)));
});

router.get('/subApps/:appName', function(req, res) {
    res.sendFile(path.resolve(__dirname, `../apps/${req.params.appName}/index.html`));
});

router.get('*', function(req, res) {
    res.sendFile(path.resolve(__dirname, '../base/index.html'));
});

app.use('/', router);

app.listen(3000, function() {
    console.log('Fileserver listening on port 3000');
});

Custom App:

创建自定义应用程序时,我要做的就是扩展router配置

export default new Router({
    base: '/my-first-custom-app/',
    mode: 'history',
    routes: [
        {
            path: '/',
            component: PageOne,
        },
        {
            path: '/two',
            component: PageTwo,
        },
    ],
});

并将index.htmlmain.jsApp.vue中的#app ID重命名为#customApp.

此外,我将该子应用的构建文件放入名为apps的文件夹中的生产目录中.


这允许我创建多个应用程序,并将它们呈现到主应用程序容器的div容器中.子应用本身不一定是Vueapply.我可以用Jquery、Angular或React创建新的应用程序,甚至只是一个简单的应用程序.html文件.

Vue.js相关问答推荐

复选框列未在vutify中排序

Vue 3组合式API如何测试是否发出正确的事件

Nuxt 和 Vite 有什么区别?

Vue Js Composition Api 未定义(读取名称)

Vue + Webpack 构建显示空白页面

即使响应为 200,也会调用 Axios Catch

Vue cli 3项目,图像路径中的动态src不起作用

Webpack 你可能需要一个合适的加载器来处理这个文件类型

可以同时使用 Tailwind css 和 Bootstrap 4 吗?

vuejs中watch方法和计算方法有什么区别

加载和渲染时对象值上的 Vue.js 未定义错误

如何从 vue.js 中的自定义组件中冒泡点击事件?

从 Url 中删除查询字符串参数

如何在 Node.js 服务器上部署 Vue.js 应用程序

如何在 Vue.js 3 中使用 Vue 3 Meta?

包含 Vue 组件的 HTML 字符串的 Nuxt 渲染函数

Vue 3 组合 API,如何在 setup() 函数中获取上下文父属性?

Vuejs 3 从子组件向父组件发出事件

我应该将所有数据存储在 vuex 状态

将 Vue props与默认值合并