javascript 无法解析“材料界面/按钮”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48789117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't resolve 'material-ui/Button'
提问by pKay
I have an existing react project to which i want to add material-ui library . So i have used the command npm install --save material-ui. But when i run it ,it shows error .
我有一个现有的 react 项目,我想向其中添加 material-ui 库。所以我使用了命令npm install --save material-ui。但是当我运行它时,它显示错误。
Here is the error details -
这是错误详细信息-
Can't resolve 'material-ui/Button' in 'C:\Users{user}\demo2\src'
无法解析“C:\Users{user}\demo2\src”中的“material-ui/Button”
Here is the link for repository
这是存储库的链接
https://github.com/mui-org/material-ui
https://github.com/mui-org/material-ui
<Button variant="raised" color="primary">
Hello World
</Button>
回答by Shubham Khatri
The Buttoncomponent that you are trying to use from material-uiis imported as Button from v1onwards which is still in beta stage. To use it you need to install it like
Button您尝试使用的组件material-ui从v1仍处于测试阶段的Button开始导入。要使用它,您需要像这样安装它
npm install --save material-ui@next
and then you can import Button from material-ui as
然后你可以从 material-ui 导入 Button 作为
import Button from 'material-ui/Button';
Check its usage as mentioned in readmeof the git repository
检查其用法,如git 存储库的自述文件中所述
In the current stable version, you have option of using FlatButton, RaisedButton, FloatingActionButtonand IconButton
在目前稳定的版本,你必须使用的选项FlatButton,RaisedButton,FloatingActionButton和IconButton
回答by Yrineu Rodrigues
For others who face the same issue in a future:
对于将来面临同样问题的其他人:
// with npm
npm install @material-ui/core
// with yarn
yarn add @material-ui/core
回答by dauffret
@Shubham Khatri's answer should be the accepted answer IMHO.
@Shubham Khatri 的答案应该是公认的答案恕我直言。
However, in order to use the Material UI library you installed, you should use it as in the example found in the MUI documentation:
但是,为了使用您安装的 Material UI 库,您应该按照 MUI 文档中的示例使用它:
import React from 'react';
import RaisedButton from 'material-ui/RaisedButton';
const style = {
margin: 12,
};
const RaisedButtonExampleSimple = () => (
<div>
<RaisedButton label="Default" style={style} />
<RaisedButton label="Primary" primary={true} style={style} />
<RaisedButton label="Secondary" secondary={true} style={style} />
<RaisedButton label="Disabled" disabled={true} style={style} />
<br />
<br />
<RaisedButton label="Full width" fullWidth={true} />
</div>
);
export default RaisedButtonExampleSimple;
Keep in mind that v1.x versions of MUI are not backward compatible with v0.x versions. MUI strongly recommends using v1.x in new projects even though it's in beta, as the amount of work needed to upgrade from v0.x to v1.x is so much more than v1.x to v1.y (been there, done that, and I agree)
请记住,v1.x 版本的 MUI 不向后兼容 v0.x 版本。MUI 强烈建议在新项目中使用 v1.x,即使它处于测试阶段,因为从 v0.x 升级到 v1.x 所需的工作量远远超过 v1.x 到 v1.y(去过那里,完成了) , 并且我同意)

