简体   繁体   English

在严格模式下,对象文字不能具有多个具有相同名称的属性

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

This is mye code: 这是mye代码:

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

The line: 该行:

trackCircuitReducer, trackCircuitReducer

Gives me: 给我:

(property) trackCircuitReducer: Reducer (属性)trackCircuitReducer:减速器

An object literal cannot have multiple properties with the same name in strict mode.ts(1117) 在严格模式下,对象文字不能具有多个具有相同名称的属性.ts(1117)

Duplicate identifier 'trackCircuitReducer'.ts(2300) 标识符重复'trackCircuitReducer'.ts(2300)

How can i solve this? 我该如何解决?

Problem is cus you are using comma instead of two dots trackCircuitReducer, trackCircuitReducer 问题是因为您使用逗号而不是两个点,所以trackCircuitReducer,trackCircuitReducer

Use this : 用这个 :

trackCircuitReducer: trackCircuitReducer trackCircuitReducer:trackCircuitReducer

In an object all keys should be followed by a : to pass the value. 在一个对象中,所有键应后跟一个:来传递值。

Change 更改

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

to

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

By having the typo , instead of : , you're using shorthand property names . 通过具有错字,而不是:你正在使用速记属性名称

So, your object literal is equivalent to: 因此,您的对象文字等同于:

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

From MDN : 从MDN

Strict mode prior to Gecko 34 requires that all properties named in an object literal be unique. Gecko 34之前的严格模式要求在对象文字中命名的所有属性都必须是唯一的。 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. 属性名称重复是严格模式下的语法错误。

(Note: This is no longer the case in ECMAScript 2015 ) (注意: ECMAScript 2015中不再是这种情况)

You can simplify your literal to: 您可以将文字简化为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM