admin管理员组文章数量:1180453
I want to use a javascript class in may Vue application.
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')
In all cases I receive when I want to call a function of the class (className.function2()
): the function is undefined.
I want to use a javascript class in may Vue application.
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')
In all cases I receive when I want to call a function of the class (className.function2()
): the function is undefined.
3 Answers
Reset to default 22You 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
}
Using import/export
, you'd use
export class className {}
and
import {className} from '<file>';
Compare to @baao answer, I add the default
keyword for the export.
export default class className {}
and in the component.vue
file:
<script>
import className from '<pathToFile>';
...
</script>
本文标签: vuejsImport javascript class in vueJSStack Overflow
版权声明:本文标题:vue.js - Import javascript class in vueJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738105819a2064360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
require
right? – Feathercrown Commented May 7, 2018 at 13:09function2
does not work, doesfunction1
work? – kwyntes Commented May 7, 2018 at 13:09