admin管理员组文章数量:1379438
I have two files: User.js
and Login.js
. After login is successfull i want to call static logIn
method of User class. I have strange behaviour. What am I doing wrong?
File contents User.js:
// user/User.js
// I also tried export default class User
export class User {
static logIn (token) {
}
static logOut (token) {
}
static isAuthorized () {
}
}
And Login.js:
// login/Login.js
import React from 'react';
import GoogleLogin from 'react-google-login';
// I also tried import User from './../user/User';
// I also tried import {User} from './../user/User';
import * as User from './../user/User';
class Login extends React.Component {
constructor (props, context) {
super(props, context);
}
responseSuccess (googleUser) {
const idToken = googleUser.getAuthResponse().id_token;
User.logIn(idToken);
}
///
}
export default Login;
When I use import and export this way I get this behaviour:
So, User is an object with property User
. This property contains all methods of class User.
Is it possible to somehow export/import class so I will get user class methods in User
object?
Now there is only one way to use methods: User.User.logIn()
.
I have two files: User.js
and Login.js
. After login is successfull i want to call static logIn
method of User class. I have strange behaviour. What am I doing wrong?
File contents User.js:
// user/User.js
// I also tried export default class User
export class User {
static logIn (token) {
}
static logOut (token) {
}
static isAuthorized () {
}
}
And Login.js:
// login/Login.js
import React from 'react';
import GoogleLogin from 'react-google-login';
// I also tried import User from './../user/User';
// I also tried import {User} from './../user/User';
import * as User from './../user/User';
class Login extends React.Component {
constructor (props, context) {
super(props, context);
}
responseSuccess (googleUser) {
const idToken = googleUser.getAuthResponse().id_token;
User.logIn(idToken);
}
///
}
export default Login;
When I use import and export this way I get this behaviour:
So, User is an object with property User
. This property contains all methods of class User.
Is it possible to somehow export/import class so I will get user class methods in User
object?
Now there is only one way to use methods: User.User.logIn()
.
-
When you say
import * as <NAME> from <LOC>
, if says "take all of the exports from file 'x' and collect them into one object and export that object as <NAME>". Should work if you just change your import statement toimport User from './../user/User'
since the class is your default export – m_callens Commented Feb 2, 2017 at 21:17 -
Yes, it is logical explanation. I understand it. The problem it does not work... I also tried
import User from './../user/User'
, I just added one of the variants there. – Sharikov Vladislav Commented Feb 2, 2017 at 21:35 - The example and the screenshot show different code. – a better oliver Commented Feb 3, 2017 at 9:33
3 Answers
Reset to default 5You are using a default export, so with a namespace import (* as User
) you'd have to use User.default
to access the class.
Instead, use a default import:
import User from './../user/User';
However, your screenshot suggests that you're actually doing a named export with export class User { … }
, not a default export. If you want to use that, you'd have to import the name:
import {User} from './../user/User'; // short for {User as User}
That said, you probably shouldn't be using a class consisting of only static methods anyway. Use multiple named exports
export function logIn(token) {
}
export function logOut(token) {
}
export function isAuthorized() {
}
and then use the namespace import
import * as User from './../user/User';
to access them as User.logIn
etc.
Since you are using a default export, there is no need to specify an alias via as
.
Instead, use the following syntax to import the default export of the module:
import User from './../user/User';
More info in the MDN docs.
import User from './../user/User';
should be enough, you dont have more exported values from that file
本文标签: javascriptHow to create class in one es6 module and import it in anotherStack Overflow
版权声明:本文标题:javascript - How to create class in one es6 module and import it in another? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744416850a2605241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论