在 vueJS 中导入 javascript 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50215005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import javascript class in vueJS
提问by Kutas Tomy
I want to use a javascript class in may Vue application.
我想在 Vue 应用程序中使用 javascript 类。
My class looks like:
我的课看起来像:
class className {
constructor() {
...
}
function1() {
...
}
static funtion2() {
...
}
}
I tried to import this class in my application like:
我试图在我的应用程序中导入这个类,例如:
- import className from './fileName.js';
- var {className} = require('./fileName.js')
- 从 './fileName.js' 导入 className;
- var {className} = require('./fileName.js')
In all cases I receive when I want to call a function of the class (className.function2()): the function is undefined.
在所有情况下,当我想调用类 ( className.function2()) 的函数时都会收到:该函数未定义。
采纳答案by Tnc Andrei
You need to export the class to be able to import/require it
您需要导出类才能导入/需要它
//1. For import syntax
export default class className {...}
//2. For require syntax
class className {}
module.exports.className = className
//or
module.exports = {
className: className
}
回答by baao
Using import/export, you'd use
使用import/export,你会使用
export class className {}
and
和
import {className} from '<file>';

