简体   繁体   English

Vue-Cli - 如何在 index.html 中加载外部文件(从根目录中)

[英]Vue-Cli - How to load external files (out of root) in index.html

First of all, I have a folder structure like this (and can't modify):首先,我有一个这样的文件夹结构(并且不能修改):

|- base_shared |- base_shared
|- controls |- 控制
|- .js files |- .js 文件
|- libs |- 库
|- .js files |- .js 文件
|- Project A (created with Vue-Cli) |-项目 A (使用 Vue-Cli 创建)
|- Project B (created with Vue-Cli) |-项目 B (使用 Vue-Cli 创建)

Inside base_shared folder we store all of our files that need to be shared with all projects, it includes libs, UI controls, etc... Those are, in majority, .js files and don't have any kind of export default or similar, since they are simple.js.base_shared文件夹中,我们存储了需要与所有项目共享的所有文件,它包括库、UI 控件等......这些大多数是.js文件,没有任何类型的export default或类似文件,因为它们是 simple.js。 Those files are frequently modified and frequently there are new files added in base_shared that all projects can consume.这些文件经常被修改,并且经常在base_shared中添加所有项目都可以使用的新文件。

Folders Project A/B are Vue-Cli created projects, with own vue.config.js, routes and .vue components.文件夹Project A/B是 Vue-Cli 创建的项目,具有自己的 vue.config.js、路由和.vue组件。 In each project we have a public/index.html file.在每个项目中,我们都有一个public/index.html文件。 Here we load big part of our libraries, .css and.js files.在这里,我们加载了大部分库,.css 和 .js 文件。 (not using import() , but using <script src=...> in the head) (不使用import() ,但在头部使用<script src=...>

My question is:我的问题是:
How can I load files that are out of the <%= BASE_URL %> (in this case, files from base_shared ) in the index.html of each project?如何在每个项目的 index.html 中加载 <%= BASE_URL %> 之外的文件(在这种情况下,来自base_shared的文件)? All those files need to be globally accessible in all parts/views/components of the project during runtime.所有这些文件都需要在运行时在项目的所有部分/视图/组件中全局访问。

I tried many things like:我尝试了很多事情,例如:
src="../base_shared/controls..."
src="<%= BASE_URL %>../base_shared/controls...
but obviously it won't work since it is out of root/host scope (let's assume for now that host is http://localhost:8081/) I'm also using in vue.config.js the webpack with a resolve.alias to as '@base': path.resolve(__dirname, '../base_shared')但显然它不会工作,因为它不在根/主机 scope (让我们现在假设主机是 http://localhost:8081/)我也在 vue.config.js 中使用resolve.alias'@base': path.resolve(__dirname, '../base_shared')

I'm used to IIS servers where I can create a "Virtual Directory" to point to base_shared and then use it in my project like the folder was inside the project root (when it is not, it's virtual)我习惯了 IIS 服务器,在那里我可以创建一个“虚拟目录”来指向base_shared ,然后在我的项目中使用它,就像文件夹在项目根目录中一样(如果不是,它是虚拟的)

There's something like that in Vue/Node?在 Vue/Node 中有类似的东西吗? Can I configure a virtual path?我可以配置虚拟路径吗? Or I need to rethink the idea?还是我需要重新考虑这个想法?

Did you try to set this into your vue.config.js?您是否尝试将其设置到您的 vue.config.js 中?

const path = require('path')

module.exports = {
    chainWebpack: config => {
        config.resolve.alias
            .set('libs', path.resolve(__dirname, '../base_shared/'))
    }
}

Them you use an import.... from.... into your script block.他们使用import.... from....到您的脚本块中。

<script>
   import { packagename } from "packagefolder/package.js"
   import 'packagefolder/css/package.css'

   export default {
       ...       
   }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM