【问题标题】:Using material-ui with ecmascript6将 material-ui 与 ecmascript6 一起使用
【发布时间】:2015-10-09 13:47:15
【问题描述】:

我刚刚找到material-ui,我正在尝试通过以下方式添加一些组件

import React from 'react';
import mui from 'material-ui';
import injectTapEventPlugin from 'react-tap-event-plugin';
import PropertiesList from './../components/propertylist.jsx';
import Filters from './../components/filter.jsx';
import Properties from './../models/PropertiesModel.js';
import Toolbar from './../components/toolbar.jsx';
var AppBar = mui.AppBar;
var IconButton = mui.IconButton;
var NavigationClose = mui.NavigationClose;
var ThemeManager = new mui.Styles.ThemeManager();
var FlatButton = mui.FlatButton;
injectTapEventPlugin();


class TransProperties extends React.Component {

  constructor(props){
     super(props);
   }

  render() {
    return <div className="row">
        <div className="col-lg-9">
            <AppBar
              title="Title"
              iconElementLeft={<IconButton><NavigationClose /></IconButton>}
              iconElementRight={<FlatButton label="Save" />} />
            <PropertiesList url="/properties.json"/>
        </div>
        <div className="col-lg-3">
            <Filters/>
        </div>
    </div>;
  }

   handleTouchTap() {
        alert("oh hi");
    }
}

export default TransProperties

但我收到错误Uncaught TypeError: Cannot read property 'spacing' of undefined

指的是

 getStyles: function getStyles() {
    var spacing = this.context.muiTheme.spacing;...}

我错过了什么?

【问题讨论】:

    标签: reactjs ecmascript-6 material-ui


    【解决方案1】:

    您可能缺少组件上的contextType,这使得muiTheme 可用。

    您的组件应如下所示:

    class ExampleComponent extends React.Component {
    
      getStyles() {
        ...
        let spacing = this.context.muiTheme.spacing;
        ...
      }
    
      render() {
        return (
          <div style={this.getStyles()} />
        );
      }
    }
    
    // Don't forget this little nugget
    ExampleComponent.contextTypes = {
      muiTheme: React.PropTypes.object
    }
    

    【讨论】:

    • 感谢您的反馈,是的,这似乎是可行的方法,但我得到了 Unexpected token
    • 您可以发布您遇到问题的组件吗?也许还有其他原因导致Unexpected token 问题?
    • 我有你提供给我的链接非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2020-01-20
    • 2022-06-22
    • 2021-03-22
    • 2021-01-14
    相关资源
    最近更新 更多