繁体   English   中英

未捕获的 ReferenceError:say 未定义

[英]Uncaught ReferenceError: say is not defined

我尝试将我的function hello()从 hello.js 导入到我的页面 sign_up.liquid。 我浏览了我的 main.js 以获得一个 bundle.js( browserify ./public/js/main.js -o ./public/js/bundle.js )但我有一个未捕获的 ReferenceError 。 为什么我不能在 sign_up.liquid 中使用我的function 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 是你的导出,所以你的导入 hello() 变成了 say。

如果你只使用

say() 

你应该得到 hello() 函数的值。

或者,您可以使用解构方法为导出指定别名。

像这样的东西:

const { hello: say } = require('./hello.js');

此处有关解构的更多信息 具体而言,您可能需要查看名为:分配给大约页面一半左右的新变量名称的部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM