簡體   English   中英

JavaScript es6 從另一個類調用靜態函數

[英]JavaScript es6 call static function from another class

這是帶有靜態函數的類

import alt from '../alt';
import Parse from 'parse';
import { ElementTypes } from '../constants/ElementTypes';

class BoardActions {

    static getDefaultElement(x, y) {
        var Element = Parse.Object.extend("Element");
        var element = new Element();
        element.set("x", x);
        element.set("y", y);
        return element;
    }
}

export default alt.createActions(BoardActions);

這就是調用靜態函數const startElement = BoardActions.getDefaultElement(0, 3);

import alt from '../alt';
import Parse from 'parse';
import { ElementTypes } from '../constants/ElementTypes';
import BoardActions from './BoardActions';

class ProjectActions {

    createNewProject(name) {
        return (dispatch) => {
            dispatch();
            const Project = Parse.Object.extend("Project");
            const project = new Project();
            let projectObject = null;
            project.set('name', name);
            project.save().then((object) => {
                projectObject = object;
                const startElement = BoardActions.getDefaultElement(0, 3);
                startElement.set('type', ElementTypes.StartType);
                startElement.set('root', true);
                startElement.set('projectId', object.id);
                return startElement.save();
            }).then((object) => {
                this.newProjectCreated(projectObject);
            }, (error) => {
                this.parseError(error);
            });
        }
    }

}

export default alt.createActions(ProjectActions);

我收到此錯誤:

ProjectActions.js:69 Uncaught TypeError: _BoardActions2.default.getDefaultElement is not a function

怎么了?

編輯:我使用 babel 作為轉譯器。

"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.0",
"babel-preset-survivejs-kanban": "^0.3.3",

編輯(因為你編輯了你的問題):

在您要導入的第二個文件中

import BoardActions from './BoardActions';

這是從“./BoardActions”導入默認值。

查看第一個文件,您正在導出函數的結果而不是類本身。

export default alt.createActions(BoardActions);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM