如何将带有静态项的 class 转换为模块文件
How to convert class with static items to module file
我有一个 class 这样的:
export default class {
static myConnection = GHD.initConnection();
static getCards = () => this.myConnection.getCards();
}
我称之为:
import connector from '../path.js';
connector.getCards();
我想将带有静态项目的 class 转换为模块,基本上我不想使用 class。
这可能吗?
我试过类似的东西:
module.exports {
myConnection: GHD.initConnection();
getCards: () => this.myConnection.getCards();
}
但这不起作用。
使用评论中要求的真实代码值进行更新:
export default class {
static firebaseReference = RNFirebase.initializeApp();
static getDatabase = () => this.firebaseReference.database();
static getUser = () => this.firebaseReference.auth().currentUser;
}
您可能正在寻找
export const myConnection = GHD.initConnection();
export const getCards = () => myConnection.getCards();
然后
import * as connector from '../path.js';
connector.getCards();
或
import { getCards } from '../path.js';
getCards();
如果必须保持与默认导入语法的兼容性,可以使用
export const myConnection = GHD.initConnection();
export const getCards = () => myConnection.getCards();
/** @deprecated */
export default { myConnection, getCards };
我有一个 class 这样的:
export default class {
static myConnection = GHD.initConnection();
static getCards = () => this.myConnection.getCards();
}
我称之为:
import connector from '../path.js';
connector.getCards();
我想将带有静态项目的 class 转换为模块,基本上我不想使用 class。
这可能吗?
我试过类似的东西:
module.exports {
myConnection: GHD.initConnection();
getCards: () => this.myConnection.getCards();
}
但这不起作用。
使用评论中要求的真实代码值进行更新:
export default class {
static firebaseReference = RNFirebase.initializeApp();
static getDatabase = () => this.firebaseReference.database();
static getUser = () => this.firebaseReference.auth().currentUser;
}
您可能正在寻找
export const myConnection = GHD.initConnection();
export const getCards = () => myConnection.getCards();
然后
import * as connector from '../path.js';
connector.getCards();
或
import { getCards } from '../path.js';
getCards();
如果必须保持与默认导入语法的兼容性,可以使用
export const myConnection = GHD.initConnection();
export const getCards = () => myConnection.getCards();
/** @deprecated */
export default { myConnection, getCards };