gpt4 book ai didi

jquery - RequireJS 使用其他文件中的路径

转载 作者:行者123 更新时间:2023-12-01 01:48:00 26 4
gpt4 key购买 nike

我有一个用 cakephp、backbone 和 requirejs 开发的网站。

在我的配置文件中,我已经映射了我的 javascript,如果我将 jquery 调用到该文件中,则一切正常。如果我将 jquery 调用到另一个文件中,则会出现错误,该函数未定义,就像 jquery 未加载一样。

这是我的 config.js 工作正常的文件:

require.config({
baseUrl: "/site.com/js/",

paths: {
// Aliases for libraries, so that we can change versions from here
jquery: "libs/jquery/jquery-2.0.2.min",
handlebars: "libs/backbone/template/handlebars",
lodash: "libs/backbone/template/lodash.min",
backbone: "libs/backbone/backbone-1.0.0.min",
less:"libs/less/less-1.4.0-beta.min",
helper:"app/helper",
jquery_cookie:"libs/jquery/plugin/jquery.cookie",
text:"libs/require/text-2.0.7"
},

shim: {
"lodash": { exports: '_' },
"handlebars": { exports: 'Handlebars' },
"jquery": { exports: '$' },
"backbone": {
//These script dependencies should be loaded before loading
//backbone.js
deps: ["helper", "lodash", "jquery"],
//Once loaded, use the global "Backbone" as the
//module value.
exports: "Backbone"
},
"jquery_cookie": {
deps: ["jquery"]
//,exports:"$"
},
"less": {
exports:"less"
}
}

// This is appended to every module loading request, for cache invalidation purposes
// comment it on production
, urlArgs: "bust=" + (new Date()).getTime()
});

require(["jquery"], function ($) {
$("body").empty();
});

如果我将其写入 config.js

require.config({
baseUrl: "/site.com/js/",

paths: {
// Aliases for libraries, so that we can change versions from here
jquery: "libs/jquery/jquery-2.0.2.min",
handlebars: "libs/backbone/template/handlebars",
lodash: "libs/backbone/template/lodash.min",
backbone: "libs/backbone/backbone-1.0.0.min",
less:"libs/less/less-1.4.0-beta.min",
helper:"app/helper",
jquery_cookie:"libs/jquery/plugin/jquery.cookie",
text:"libs/require/text-2.0.7"
},

shim: {
"lodash": { exports: '_' },
"handlebars": { exports: 'Handlebars' },
"jquery": { exports: '$' },
"backbone": {
//These script dependencies should be loaded before loading
//backbone.js
deps: ["helper", "lodash", "jquery"],
//Once loaded, use the global "Backbone" as the
//module value.
exports: "Backbone"
},
"jquery_cookie": {
deps: ["jquery"]
//,exports:"$"
},
"less": {
exports:"less"
}
}

// This is appended to every module loading request, for cache invalidation purposes
// comment it on production
, urlArgs: "bust=" + (new Date()).getTime()
});

将对 jquery 的调用移至 default.ctp 中,以获取函数未定义的错误,因为我认为未加载 jQuery。

默认.ctp

require(["jquery"], function ($) {
$("body").empty();
});

为什么我不能在 deafult.ctp 中使用 jquery?我必须映射到所有文件吗?我不这么认为,有人可以帮助我吗?谢谢

最佳答案

您的 config.js 似乎在您尝试使用 jquery 后运行。尝试在配置文件运行时检查日志。

关于jquery - RequireJS 使用其他文件中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262783/

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