【问题标题】:React Native "is not a function"React Native “不是函数”
【发布时间】:2016-05-29 18:09:20
【问题描述】:

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

这是代码:

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();

【问题讨论】:

    标签: javascript react-native ecmascript-6


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-27
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2017-02-24
      相关资源
      最近更新 更多