javascript - 将 Vue 应用挂载到主 Vue 应用的容器中

标签 javascript vue.js

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


基本思想: 应该提供一个 REST API 和一个基本容器(Vue 应用程序),它能够在特定的 div 元素中呈现其他 Vue 应用程序。这将允许其他人添加他们自己的前端应用程序并在需要时使用该 API。所以我只会提供一些基本的应用程序。主要思想是为非常模块化的管理系统创建一个即插即用系统。我将为该自定义应用程序提供注册流程,以便 API 知道该应用程序及其基本 URL,而我的基本应用程序可以获取所有这些应用程序。

enter image description here


基本应用设置

所以我的基础应用程序为这些自定义应用程序提供了路径

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

并将渲染这个 View

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

我的导航栏可以提供链接 /app/one 来导航和呈现那个 customAppContainer 中名为“one”的应用。


自定义应用设置

在定义我的路线时,我必须设置 base url

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

但对于 ma​​in.js 我不确定如何设置它。我想我必须将其更新为类似

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

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


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

一个类似的例子是 Nextcloud

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

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

最佳答案

1

Vue does not know CustomAppContainer

尝试添加import CustomAppContainer from "path/to/CustomAppContainer.vue"
它必须在 .vue 文件中。


2

Vue does not know #customAppContainer

是的,该选择器必须在 index.html 中.你的在 CustomAppContainer . 尝试添加到 index.html (通常在/public 文件夹中)类似于 <div id="#app"></div>并替换 .$mount('#customAppContainer');.$mount('#app');


3

Vue 路由器需要 <router-view>标签。所以,试试这个标记:

<!-- In template is all page -->
<template>
  <!-- Some stuff as navigation... -->
  <nav>
    <!-- Router links - renders as "a" element -->
    <router-link to="/one">To /one</router-link>
    <!-- Or use standard tags - they're also good -->
    <a href="/two">To /two</a>
  </nav>
  <!-- In this component would be component chosen in router (Home.vue) -->
  <router-view/>
</template>

4

Router.js 需要导入:

import Home from "path/to/the/Home.vue"
export default new Router({
  base: '/one/',
  routes: [
    {
      path: '/',
      component: Home,
    },
  ],
});

一切都好吗?

关于javascript - 将 Vue 应用挂载到主 Vue 应用的容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58131480/

相关文章:

javascript - 变异数组 Vuex

arrays - 在 PostgreSQL 中将图像文件保存为 bytea 并检索它们以在 HTML 模板上显示

javascript - 如何处理 Yeoman 的 package.json 冲突错误?

javascript - 在 javascript 中创建脚本

image - SVG 中的 <image> 中的 VueJS 外部引用没有 vueloader?

javascript - 如何用两个过滤器制作一个过滤器?

javascript - Vue Js v 绑定(bind) :class syntax not working ?

javascript - 查找字符串中第一个最长的单词,不包括符号

javascript - C# Protractor AngularJS IEDriverServer Click() 异常 "Timed out waiting for page to load"

javascript - 具有约束的数据动态聚合