React Native"不是一个功能"

时间:2016-05-29 18:09:21

标签: javascript react-native ecmascript-6

当我导出一个类作为'导出默认的AvatarStore;'与'导出默认的新AvatarStore();'我尝试在其他类中使用其方法时出现以下错误...使用新语法是否正确?

not a function 这是代码:

import { List } from 'immutable';
    import EventEmitter from 'events';
    import Utils from '../utils/Utils.js'
    import RestService from '../services/RestService'
    import RestCallStatus from '../constants/RestCallStatus'
    import Avatar from '../models/Avatar'

    const CHANGE_EVENT = 'change';
    const TAG = 'AvatarStore';


    class AvatarStore extends EventEmitter {

    constructor() {
        super();
        this._populateRestCallStatus = RestCallStatus.NOT_REQUESTED;
        this._populated = false;
        this._dataStore = List();
    }

    populate(){
        RestService.getAllAvatars(this.handleSuccess.bind(this), this.handleFailure.bind(this));
        this._populateRestCallStatus = RestCallStatus.STARTED
    }

    handleSuccess(serviceName, jsonData){
        Utils.logMethod(TAG, 'handleSuccess ' + serviceName);
        if(jsonData.length > 0){  this._dataStore = List().clear(); }
        jsonData.forEach((entity) => {
            this._dataStore = this._dataStore.push(new Avatar(entity))
        });
        this._populated = true;
        this._populateRestCallStatus = RestCallStatus.SUCCESS;
        this.emitChange();
    }

    handleFailure(serviceName, error){
        Utils.logMethod(TAG, 'handleFailure');
        this._populateRestCallStatus = RestCallStatus.FAILED
        console.error(`Server call ${serviceName} failed with error: ${serviceName}!`)
    }

    getItems(){
        //Utils.logMethod(TAG, 'getItems');
        return this._dataStore;
    }

    getItemById(itemId){
        return Utils.findArrayElementById(this._dataStore, itemId);
    }

    getPopulated(){
        return this._populated;
    }

    addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    }

    removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }

    emitChange() {
        Utils.logMethod(TAG, 'emitChange');
        this.emit(CHANGE_EVENT);
    }
}
export default new AvatarStore();

1 个答案:

答案 0 :(得分:1)

export default AvatarStore表示您要导出类。在export default new AvatarStore()情况下,您导出类(对象)的实例。因此,您需要使用在您的情况下有意义的那个 - 如果您想拥有更多AvatarStore实例,请使用第一个,否则您可以使用第二个。

当然,在第一种情况下,您需要在导入后在某处创建新实例。