首页 > 解决方案 > vue如何从node读取js文件

问题描述

我正在使用节点 10.16.0 和 vue 2.6.11,我想在节点根文件夹中有一个 config.js 文件,指向节点结构之外的文件夹

hello
   hi.txt
node-project //node project
   server.js
   config.js 
   public //contains build vue
   vue-project //contains not build vue

config.js 应该包含类似

let txtFile= '../hello/hi.txt'; 
exports.txtFile = txtFile;

我希望能够将 config.js 文件保留在节点中,而不是混合在 vue 中,但构建 vue(在公共文件夹内)也能够读取它

理想情况下,我想从 vue-project 读取 config.js,当我这样做时npm run build,我还可以从 public 读取 config.js(我放置构建的 vue 项目的地方)

这是我失败的,过于复杂的尝试

由于您放入 vue 项目的公共文件夹中的所有内容都会传递给构建,并且在构建过程中路径会自动更改,所以我这样做了

hello
   hi.txt
node-project //node project
   server.js  
   vue-project //contains not build vue
      public
         configs
             config.js //contains ../../../../hello/hi.txt
      src
        views
           Home.vue //imports configs/config.js

当我这样做时npm run build,上面变成了

hello
   hi.txt
node-project //node project
   server.js 
   public //contains build vue
      css
      js
      fonts
      configs
         config.js
      index.html

于是 config.js 自动转移到了 build vue 中,config.js 的路径自动改变了,config.js 在节点中,方便访问,只有一个子文件夹,没有混入 vue js。

问题是现在 config.js 改变了位置,所以我必须手动将其内容更改为 let txtFile= '../../hello/hi.txt';

我认为它没关系,付出的代价很小,它只是一次手动更改。

但问题是 Vue 没有读取更改后的值。它仍然读取../../../../hello/hi.txt而不是'../../hello/hi.txt。重新启动节点没有帮​​助。我猜vue缓存了这个值?

请指教。我希望我的例子能帮助你理解我想要做什么。随时问我任何可以帮助您澄清的问题

谢谢

标签: node.jsvue.jsconfigurationvue-cli

解决方案


推荐阅读