簡體   English   中英

Vue 打字稿導入依賴

[英]Vue typescript import dependency

我正在嘗試將彩虹代碼節點模塊添加到我使用 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>

“默認情況下 babel-loader 忽略 node_modules 中的所有文件”

所以知道我會嘗試將包添加到 vue.config.js 中的 transpileDependencies

module.exports = {
  
  ...

  transpileDependencies: [
    'rainbow-code'
    // you may need to change the path/file name, Rainbow?
  ],

  ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM