在Vue应用程序中导入脚本后未定义变量

时间:2018-05-13 19:20:55

标签: javascript vue.js

我正在尝试将JavaScript文件导入我的Vue应用程序(在main.js中),但控制台显示Uncaught ReferenceError: CI is not defined

CI是Defines.js中的变量,这是第三方库,遗憾的是我无法进行任何修改。请参阅以下代码:

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './afw/Utils/Defines.js' // <<< this line!

console.log(CI) // <<< I got the error here!

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app') 

由于Defines.js是一个包含敏感信息的私人脚本,因此我无法向您展示太多内容。但是,这是此脚本的一部分:

var CI = {};

...

if (typeof module !== 'undefined') {
    module.exports = CI.Utils.Define;
}

我该怎么做才能让它发挥作用?如果我转移到传统的Web应用程序(使用<script/>导入脚本),它可以工作,但我需要使用VueJS 2.

更新

当我导入需要Defines.js中的变量的另一个JavaScript时,我也遇到了同样的问题:

import './afw/Utils/Defines'
import './afw/System/Core.js' // <<< Core.js needs a variable inside the Defines.js

1 个答案:

答案 0 :(得分:0)

您正在公开CI.Utils.Define内部的内容,而不是CI本身。除非您从CI内导出./afw/Utils/Defines.js,否则无法解决此问题。

当你通过脚本将它包含在网页中时,它会起作用,因为那里没有模块的概念,CI最终在window对象上声明,但正如你所注意到的那样,这不是使用模块。