我尝试将我的函数hello()
从hello.js导入到我的页面sign_up.liquid。
我浏览器化我的 main.js 以获得一个bundle.js(browserify ./public/js/main.js -o ./public/js/bundle.js
),但我有一个 Uncaught ReferenceError 。为什么我无法在 sign_up.liquid 中使用我的函数 hello()
?
--hello.js
var hello = function(){
console.log('I said Hello')
}
module.exports = hello;
--main.js
const say = require('./hello.js')
--bundle.js
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var hello = function(){
console.log('I said Hello')
}
module.exports = hello;
},{}],2:[function(require,module,exports){
const say = require('./hello.js')
},{"./hello.js":1}]},{},[2]);
--sign_up.liquid
[...]
<script src="../js/bundle.js"></script>
<script>
say.hello();
<script>
最佳答案
你的问题是因为 Hello 是你的导出,所以你的 import hello() 变成了 say。
如果您只使用
say()
您应该获得 hello() 函数的值。
或者,您可以使用解构方法为导出指定别名。
类似这样的事情:
const { hello: say } = require('./hello.js');
有关 Destructuring here 的更多信息 具体来说,您可能需要查看位于页面中间位置的名为:分配给新变量名称的部分。
关于javascript - Uncaught ReferenceError : say is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59406464/