我正在尝试使用从 npm 安装的 React Material-ui 主题,当我包含“从“material-ui/styles/MuiThemeProvider”导入 MuiThemeProvider;”时,出现以下错误在 boot-client.tsx 中:
TS7016:找不到模块的声明文件 'material-ui/styles/MuiThemeProvider'。 'W:/web/WebFront/node_modules/material-ui/styles/MuiThemeProvider.js' 隐式具有“any”类型。 如果存在,请尝试
或添加新的 包含npm install @types/material-ui/styles/MuiThemeProvider
的声明 (.d.ts) 文件declare module 'material-ui/styles/MuiThemeProvider';
我尝试了这两种建议均无效,包括运行命令:npm install -D @types/material-ui。
node_modules 中我的 @types 文件夹存在相关类型。
这是我尝试使用它的代码:
import './css/site.css';
import 'bootstrap';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import { createBrowserHistory } from 'history';
import configureStore from './configureStore';
import { ApplicationState } from './store';
import * as RoutesModule from './routes';
let routes = RoutesModule.routes;
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
// Create browser history to use in the Redux store
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
const history = createBrowserHistory({ basename: baseUrl });
// Get the application-wide store instance, prepopulating with state from the server where available.
const initialState = (window as any).initialReduxState as ApplicationState;
const store = configureStore(history, initialState);
function renderApp() {
// This code starts up the React app when it runs in a browser. It sets up the routing configuration
// and injects the app into a DOM element.
ReactDOM.render(
, document.getElementById('react-app') ); }
renderApp();
// Allow Hot Module Replacement
if (module.hot) {
module.hot.accept('./routes', () => {
routes = require<typeof RoutesModule>('./routes').routes;
renderApp();
});
}
对我有用的解决方案是上面医学的答案,我在下面更详细地解释。
打开
tsconfig.json
文件。在 "types":"material-ui",
内添加
"compilerOptions": {}
如
"compilerOptions": {"types":"material-ui"}
好吧,我明白了,在“compilerOptions”下的 tsconfig.json 中,默认情况下 Visual-studio 的类型设置为 [“webpack-env”],我需要向其中添加“material-ui”,或者选择将其删除: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
使用来自同一路径的
default
导入。
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
就我而言,我克隆了存储库并使用 npm install 安装软件包,然后出现了 mui 类型问题。为了解决这个问题,我尝试了yarn install,这对我有用。