gpt4 book ai didi

javascript - 在 Cordova 中使用 require 加载模块

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:13 24 4
gpt4 key购买 nike

我是 javascript 的新手,正在尝试大量使用模块 require() 的 cordova。我为此浏览了一些教程,例如 here .我尝试了本教程中的一个非常简单的示例,但似乎缺少某些内容。

这是我的 html 代码。

 <script>
var abc = require("js/greeting.js");
function mod() {
try{

var g = abc.sayHelloInEnglish();
console.log("reached the call");
document.getElementById("modl").innerHTML = g;
}
catch(err){
console.log("error is" + err.message);
}
}
</script>
<button onclick="mod()">click</button>

这是我的 greeting.js 的代码

  //var exports = module.exports = {};

exports.sayHelloInEnglish = function() {
return "HELLO";
};

exports.sayHelloInSpanish = function() {
return "Hola";
};

当我点击按钮时,它给出了 abc 未定义的错误。我在这里缺少使用该模块的东西吗?非常感谢。

最佳答案

实际上 module.require 不适用于浏览器。您不能像在 script 标签中那样使用它。 require 用于 node.js(服务器端 javascript)。

如果你想在浏览器中使用它,你应该使用预处理。例如,您可以使用 browserify .

要了解更多信息 - 精彩的 3 分钟视频 CommonJS modules

关于javascript - 在 Cordova 中使用 require 加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30509340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com