【问题标题】:Rails, Browserify and React with coffeescriptRails、Browserify 和 React 与咖啡脚本
【发布时间】:2017-06-22 07:49:21
【问题描述】:

我正在尝试使用 npm 设置 React,因此在没有 rails-react gem 的情况下使用它。 我已经安装了 Browserify 和 react 和 react-dom。它似乎有效,我能够运行一个简单的“hello world”示例。'

现在谈谈更复杂的东西。我已经有很多用 rails-react 加载的组件,我需要转换它们。

我已经创建并初始化脚本 (init.js.coffee)

React = require('react')
ReactDOM = require('react-dom')
PhotoRoot = require('photo_root')

和一个组件(photo_root.js.coffee)

module.exports = React.createClass
  getInitialState: ->
    null
  getDefaultProps: ->
    null

  showCard: (photoId) ->
    this.refs.card.setPhotoId photoId

  render: ->
    React.DOM.div {className: 'photo-root'},
      React.createElement PhotoGrid, showCard: @showCard
      React.createElement PhotoActionCard, photoId: 615, ref: "card"

这两个文件在同一个文件夹中。

当我运行它时,我从 BrowserifyRails::BrowserifyError: 收到一个错误:

Error while running `/Users/mh/rails_projects/phototank/node_modules/.bin/browserifyinc -t coffeeify --extension=".js.coffee" --list --cachefile=/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/browserifyinc-cache.json -o "/Users/mh/rails_projects/phototank/tmp/cache/browserify-rails/output20170205-9947-edkf1y" -`:

events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: Cannot find module 'photo_root' from '/Users/mh/rails_projects/phototank/app/assets/javascripts/components'
  at /Users/mh/rails_projects/phototank/node_modules/browser-resolve/node_modules/resolve/lib/async.js:46:17

它正在正确的位置寻找“photo_root”,但没有找到。是否与扩展有关。我正在使用咖啡脚本!

欣赏!!

【问题讨论】:

    标签: ruby-on-rails reactjs npm coffeescript browserify


    【解决方案1】:

    好吧,这很简单:

    PhotoRoot = require('photo_root')
    

    应该是

    PhotoRoot = require('./photo_root')
    

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多