在基本的 electon-vue 应用程序的上下文中,我想创建自己的 javascript 类并将其用于主进程或渲染器或 vue 组件。
我创建了 JS 类,但我从来没有找到导出我的类的好方法。
编写导入/导出模块的所有可能性都在网络中找到,但都以相同的错误完成:未定义的导出
"use strict"
import fs from 'fs'
import typeorm from 'typeorm'
import Public from './../entity/Public'
class ConnectionManager
{
constructor(){}
getConnection(type, name, options) {
}
}
module.exports = ConnectionManager
但看起来其他 js 文件与 vue-router js 完美地工作,用于路由到 vue.js 应用程序:
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: require('@/components/Home').default
}
]
})
我用 Webpack 和 libraryTarget 打包我的代码输出是:commonjs2
我似乎将 babel-loader 与 webpack 一起使用
节点版本:10.13.0
电子:3.0.10
通天塔:6
编辑 :
我试试这个语法类 js 文件:
"use strict"
import * as fs from 'fs'
import * as typeorm from 'typeorm'
import {Public} from './../entity/Public'
export default class ConnectionManager
{
constructor(){}
getConnection(type, name, options) {
}
}
使用此导入语法:
import ConnectionManager from './../service/connectionManager'
但是当我在电子中执行代码时出现此错误:
未捕获的类型错误:_service_connectionManager__WEBPACK_IMPORTED_MODULE_8__.default.getConnection 不是函数
我控制台记录了这个服务类“ConnectionManager”,我得到了这个结果(所以它确实存在):
ƒ ConnectionManager() {
babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_0___default()(this, ConnectionManager);
}
似乎最终的 js 模块 webpack 包含 ConnectionManager 类
您似乎以错误的方式将 commonjs 模块与 ES 模块混合在一起。
有很多模块(包括内置节点)没有默认的 export。要导入这样的模块,您需要在导入语句中使用* as moduleAlias
或{ exportedField }
。尝试通过这种方式重写您的代码:
import * as fs from 'fs'
import * as typeorm from 'typeorm'
import { Public } from '../entity/Public'
export default class ConnectionManager
{
constructor(){}
getConnection(type, name, options) {
}
}
由于此类导出为默认值,因此您可以使用以下构造将其导入为默认字段,其中 ConnectionManager 是当前作用域的别名:
import ConnectionManager from '../service/connectionManager'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句