我正在尝试在我的Rails项目中包含requirejs,并且在尝试使用它时遇到错误。我通过遵循以下教程来尝试使用require js:https://gist.github.com/eoinkelly/5778004,一切似乎都很好,但是当我尝试require
时,它开始给我错误。
在我的gemfile中,我有gem 'requirejs-rails'
,并且已运行bundle install
我的 application.js 看起来像这样
// = require require
require(['jquery'], function($) {
'use strict';
// Your awesome javascript goes here!
// **********************************
// This is just some guff to help you prove that everything works.
// You can and should delete this once everything works.
console.log('Successfully loaded jQuery version: ' + $('body').jquery);
if (typeof $.rails === 'object') {
console.log('The $ object seems to have been decorated by Rails UJS successfully. Good job!');
}
else {
console.log('Uh-oh, it seems that $ has not been decorated by Rails UJS. You might want to check this.');
}
// **********************************
});
它显示The $ object seems to have been decorated by Rails UJS successfully. Good job!
,因此看起来工作正常,但是在我的js代码中,我有以下一行:
var antlr4 = require(['acolade_parser/antlr4/index.js']);
这给我一个
的错误 Failed to load resource: the server responded with a status of 404 (Not Found)
如果我删除了需求的.js
部分(我认为这是正确的),我会得到
ReferenceError: Can't find variable: exports
这使我相信requirejs尚未加载。我的 application.html.erb 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>ACoLaDe</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= requirejs_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= render 'partials/header' %>
<%= yield %>
</body>
</html>
并且您可以看到requirejs_include_tag
在哪里,所以有人知道我为什么收到此错误吗?