首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用TS:“属性'__REDUX_DEVTOOLS_EXTENSION_COMPOSE__‘不存在于’__REDUX_DEVTOOLS_EXTENSION_COMPOSE__‘类型上的Redux __REDUX_DEVTOOLS_EXTENSION_COMPOSE__扩展时出错。”

使用TS:“属性'__REDUX_DEVTOOLS_EXTENSION_COMPOSE__‘不存在于’__REDUX_DEVTOOLS_EXTENSION_COMPOSE__‘类型上的Redux __REDUX_DEVTOOLS_EXTENSION_COMPOSE__扩展时出错。”
EN

Stack Overflow用户
提问于 2018-10-14 08:31:16
回答 10查看 52.6K关注 0票数 85

我的index.tsx上有这个错误。

属性'REDUX_DEVTOOLS_EXTENSION_COMPOSE‘不存在于“窗口”类型上。

以下是我的index.tsx代码:

代码语言:javascript
运行
复制
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import registerServiceWorker from './registerServiceWorker';

import { Provider } from 'react-redux';

import { createStore, compose, applyMiddleware } from 'redux';
import rootReducer from './store/reducers';

import thunk from 'redux-thunk';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

 const store = createStore(rootReducer, composeEnhancers(
     applyMiddleware(thunk)
 ));

ReactDOM.render(  <Provider store={store}><App /></Provider>, document.getElementById('root'));

registerServiceWorker();

我安装了@types/npm安装--保存-dev redux-devtools-扩展,我使用的是创建-反应-应用程序类型记录。非常感谢你对事先发生的一切提出建议。

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2021-07-01 14:37:11

这就是如何在类型记录react应用程序中使用redux-dev-tools的方法。

Window对象创建全局接口:

代码语言:javascript
运行
复制
declare global {
  interface Window {
    __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
  }
}

然后,按照以下方式创建composeEnhancers

代码语言:javascript
运行
复制
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

然后创建一个store

代码语言:javascript
运行
复制
const store = createStore(rootReducers, composeEnhancers());

rootReducers --在我的例子中,指的是在代理文件中创建的combinedReducers

现在,您可以像往常一样在Provider中使用React.js如下:

代码语言:javascript
运行
复制
ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

index.tsx中的所有代码

代码语言:javascript
运行
复制
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import rootReducers from "./reducers";

import { Provider } from "react-redux";
import { createStore, compose, applyMiddleware } from "redux";

declare global {
  interface Window {
    __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
  }
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(rootReducers, composeEnhancers());

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);
reportWebVitals();
票数 25
EN

Stack Overflow用户

发布于 2018-10-14 09:05:24

这是这个问题的一个特例。Redux不为__REDUX_DEVTOOLS_EXTENSION_COMPOSE__提供类型,因为这个函数是由Redux DevTools公开的,而不是Redux本身。

要么是:

代码语言:javascript
运行
复制
const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] as typeof compose || compose;

或者:

代码语言:javascript
运行
复制
declare global {
    interface Window {
      __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
    }
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

这已经由包含redux-devtools-extension类型的TypeScript包完成。如果安装了它,则应该使用它的导入,而不是手动访问__REDUX_DEVTOOLS_EXTENSION_COMPOSE__

票数 119
EN

Stack Overflow用户

发布于 2019-09-05 02:51:28

使用TypeScript最简化的方法是使用redux-devtools-扩展并将其安装为一个dev依赖项,如下所示:

代码语言:javascript
运行
复制
npm install --save-dev redux-devtools-extension

对于那些新加入redux和这些开发工具的人来说,下一步是混乱和不明确的。这些文档都有如下代码:

代码语言:javascript
运行
复制
const store = createStore(reducer, composeWithDevTools(
  applyMiddleware(...middleware),
  // other store enhancers if any
));

问题是我没有配置任何中间件,所以这是行不通的。在它最原始的用法中,这是您所需要的:

代码语言:javascript
运行
复制
import { composeWithDevTools } from 'redux-devtools-extension';

const store = createStore(myReducer, composeWithDevTools());

此时,如果单击浏览器中的扩展,并且有一个有效的redux存储,则可以检查状态。

这是使用(window as any)的另一种方法,也明确了如何使用最小形式的redux-devtools-extension

票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52800877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档