在严格模式下,一个对象字面量不能有多个同名的属性

An object literal cannot have multiple properties with the same name in strict mode

这是我的代码:

import { combineReducers } from 'redux';
import { postReducers } from './postReducers';
import { stationsReducer } from './TrackCircuitSensorDataFormReducers/StationsReducer';
import { trackCircuitReducer } from './TrackCircuitSensorDataFormReducers/TrackCircuitReducer';

export const rootReducer = combineReducers({
    posts: postReducers,
    stationsReducer: stationsReducer,
    trackCircuitReducer, trackCircuitReducer
});

export type IApplicationState = ReturnType<typeof rootReducer>;

行:

trackCircuitReducer, trackCircuitReducer

给我:

(property) trackCircuitReducer: Reducer

An object literal cannot have multiple properties with the same name in strict mode.ts(1117)

Duplicate identifier 'trackCircuitReducer'.ts(2300)

我该如何解决这个问题?

问题是因为您使用的是逗号而不是两个点 trackCircuitReducer, trackCircuitReducer

使用这个:

trackCircuitReducer: trackCircuitReducer

在一个对象中,所有的键都应该跟着一个 : 来传递值。

改变

export const rootReducer = combineReducers({
    posts: postReducers,
    stationsReducer: stationsReducer,
    trackCircuitReducer, trackCircuitReducer
});

export const rootReducer = combineReducers({
    posts: postReducers,
    stationsReducer: stationsReducer,
    trackCircuitReducer: trackCircuitReducer
});

输入错误 , 而不是 :,您使用的是 shorthand property names

因此,您的对象字面量相当于:

{
    posts: postReducers,
    stationsReducer: stationsReducer,
    trackCircuitReducer: trackCircuitReducer,
    trackCircuitReducer: trackCircuitReducer,
}

From MDN:

Strict mode prior to Gecko 34 requires that all properties named in an object literal be unique. The normal code may duplicate property names, with the last one determining the property's value. But since only the last one does anything, the duplication is simply a vector for bugs, if the code is modified to change the property value other than by changing the last instance. Duplicate property names are a syntax error in strict mode.

(注意:ECMAScript 2015中不再是这种情况)

您可以将文字简化为:

export const rootReducer = combineReducers({
    posts: postReducers,
    stationsReducer,
    trackCircuitReducer
})