【问题标题】:How to use vendor React modules / components within a rails application with only react-rails gem?如何在只有 react-rails gem 的 Rails 应用程序中使用供应商 React 模块/组件?
【发布时间】:2017-07-01 16:17:59
【问题描述】:

使用this 评论,我正在尝试在我的rails 应用程序中使用Draft.js。

但我不断收到错误:

Draft.min.self-b1d4414….js?body=1:1 Uncaught ReferenceError: Immutable is not defined

谁能告诉我,我正在犯的可能很明显的错误是什么?

详情:

我只使用 react-rails 来保持简单。 Gemfile 有以下相关行:

gem 'react-rails'
gem 'bootstrap-sass'

我把Draft.min.js放在app/assets/javascripts/externallibraries/下面 和Draft.cssapp/assets/stylesheets/externallibraries 下。

相关文件,application.js:

/*
 //= require jquery
 //= require jquery_ujs
 //= require turbolinks
 //= require react
 //= require react_ujs
 //= require bootstrap-sprockets
 //= require react
 //= require react_ujs
 //= require externallibraries/Draft.min
 //= require components
 //= require_tree .
*/

还有application.css

/*
 *= require externallibraries/Draft
 *= require_tree .
 *= require_self
*/

最后我的 jsx 文件是:

class NewPost extends React.Component {
    constructor() {
        super();
        this.state = {
            editorState: EditorState.createEmpty()
        };
        this.onChange = (editorState) => this.setState({editorState});
    }

    componentDidMount(){
    }

    componentWillMount() {
    };

    componentWillUnmount() {
    }

    componentWillReceiveProps(nextProps) {
        if(JSON.stringify(this.props.some_field) !== JSON.stringify(nextProps.some_field) && nextProps.retrieved_data != null) // Check if it's a new user, you can also use some unique, like the ID
        {
            this.setState({some_field: nextProps.some_field});
        }
    } 

    render() {
        return (
            <div>
                {"My state is: " + JSON.stringify(this.state, null, "   ")}
                {"And my props are: " + JSON.stringify(this.props, null, "  ")}
                <Editor editorState={this.state.editorState} onChange={this.onChange} />
            </div>
        );
    }
}

【问题讨论】:

    标签: javascript ruby-on-rails reactjs


    【解决方案1】:

    这是我找到的解决方案。也许可以挽救某人的一天:

    1. 在 Rails 项目的根目录中安装包。而不是使用纱线似乎更好。命令是yarn add draft-js

    2. config/application.rb类中添加以下行:

      config.assets.paths &lt;&lt; Rails.root.join('node_modules', 'draft-js', 'dist')

    3. app/assets/javascripts 中将以下内容添加到application.js

      //= 需要草稿

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2018-07-25
      相关资源
      最近更新 更多