簡體   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