Vue打字稿导入依赖

时间:2020-09-30 04:32:19

标签: typescript vue.js rainbow-js

我正在尝试将Rainbow代码节点模块添加到使用TypeScript的Vue应用程序中。此应用程序是使用vue-cli生成的。

rainbow-code是未使用Typescript编写的代码语法模块 我正在尝试通过main.ts分发此依赖项。我可以只复制依赖关系并在html标记中导入相对路径。但是,我希望对Typescript中的导入模块更加熟悉。

导入依赖项时,我在chrome调试器中收到以下错误

rainbow-node.js?23f9:5 Uncaught TypeError: fs.readdirSync is not a function
    at Object.eval (rainbow-node.js?23f9:5)
    at eval (rainbow-node.js:14)
    at Object../node_modules/rainbow-code/src/rainbow-node.js (chunk-vendors.js:2224)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (main.ts:14)
    at Module../src/main.ts (app.js:1313)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at Object.1 (app.js:1374)

这是我的main.ts

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Rainbow from  'rainbow-code';
import 'rainbow-code/themes/css/github.css'
import 'rainbow-code/src/language/python.js'

Vue.config.productionTip = false

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

此外,这是源代码的片段

/* eslint-disable */
var fs = require('fs');
global.Rainbow = require('../dist/rainbow.js');

var files = fs.readdirSync(__dirname + '/language');
for (var i = 0; i < files.length; i++) {
    require('./language/' + files[i]);
}

module.exports = global.Rainbow;
delete global.Rainbow;
/* eslint-enable */

另外,我想成功导入了吗?但随后出现这样的错误

python.js?86d9:6 Uncaught ReferenceError: Rainbow is not defined
    at eval (python.js?86d9:6)
    at Object../node_modules/rainbow-code/src/language/python.js (chunk-vendors.js:1971)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/ContentBlockComponent.vue?vue&type=script&lang=ts&:14)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/ContentBlockComponent.vue?vue&type=script&lang=ts& (app.js:1007)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (ContentBlockComponent.vue?6714:1)
    at Module../src/components/ContentBlockComponent.vue?vue&type=script&lang=ts& (app.js:1194)

这里是依赖项网站https://github.com/ccampbell/rainbow#install-rainbow 我不确定还有哪些其他信息对调试有用。

编辑。这是我尝试在vue组件中调用的功能。但是我似乎无法在vue组件中访问此功能。

<script language='javascript' type='text/javascript'>
  window.onload=function(){ Rainbow.color(); };
  Rainbow.color();
</script>

1 个答案:

答案 0 :(得分:0)

“默认情况下,babel-loader会忽略node_modules内部的所有文件” source

所以知道我会尝试将包添加到vue.config.js中的transpileDependencies

align-items: center;