首先,这不是重复的!这是相同的“未定义”错误,但完全遵循 github ( https://github.com/reactjs/react-rails ) 指南,但仍然无法正常工作
宝石文件:
gem 'rails'
gem 'pg', '~> 0.15'
gem 'sass-rails'
gem 'uglifier'
gem 'coffee-rails'
gem 'therubyracer'
gem 'bootstrap-sass'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'font-awesome-sass'
gem 'sprockets-rails'
gem 'react-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
应用程序.js:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap-sprockets
//= require react
//= require react_ujs
//= require components
应用程序.html:
<!DOCTYPE html>
<html>
<head>
<title>project</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'react' %>
<%= csrf_meta_tags %>
</head>
<body>
组件/app.js.jsx:
// var React = window.ReactRailsUJS;
var Hello = React.createClass({
render: function() {
return (
<p>Hello</p>
);
}
});
ReactDOM.render(
<Hello />,
document.getElementById("react-msg")
);
console.log(1);
.html 文件:
<style type="text/css">
h1, h3 {
font-weight: bold;
}
</style>
<div class="container">
<h1>React JS</h1>
<h3 id="react-msg"></h3>
</div>
开发.rb:
Rails.application.configure do
...
# config/environments/development.rb
config.react.variant = :development
# to include react add-ons
config.react.addons = true # defaults to false
end
当我访问该页面时,它说
ReferenceError: React is not defined
我做错了什么?
如果我取消注释以下
var React = window.ReactRailsUJS;
它说 createClass 不是一个函数,并且对于所有其他 React 函数都是一样的