您的位置:首页技术文章
文章详情页

javascript - requirejs加载报错问题?

浏览:67日期:2023-02-09 16:21:28

问题描述

1、使用requirejs写了个demo.html,第一次加载页面时能够正常显示,随后刷新页面就开始报错,虽然页面还是正常显示,但是我不理解为什么会这样。2、我的另一个999.html文件(跟demo.html在同级目录下,但是页面内容比较多,其余和1中的html没有太大区别)一直报错,要么显示require is not define要么显示Script error for 'jquery'或$ is not defined,请问这是为什么,应该怎么改?

以下是文件目录:javascript - requirejs加载报错问题?

代码如下:

demo.html:

<!DOCTYPE html><html> <head><meta charset='UTF-8'><title></title><script src='https://www.haobala.com/wenda/resource/require.js'></script><script src='https://www.haobala.com/wenda/resource/main.js'></script> </head> <body><script type='text/javascript'> require([’jquery’,’weui’],function($){$(’body’).css(’background’,’red’); });</script> </body></html>

main.js:

require.config({ baseUrl:’resource/app’, paths:{’jquery’:’../lib/jquery-3.2.0.min’,’zepto’:’../lib/zepto.min’,’css’:’../lib/css.min’,’weui’:’../lib/jquery-weui.min’,’cookie’:’../lib/jquery.cookie’,’swiper’:’../lib/swiper’ }});

报错信息:javascript - requirejs加载报错问题?

999.html:

<!DOCTYPE html>

<html>

<head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' /> <title></title> <link rel='stylesheet' type='text/css' href='https://www.haobala.com/wenda/resource/css/weui.css' /> <link rel='stylesheet' type='text/css' href='https://www.haobala.com/wenda/resource/css/jquery-weui.min.css' /> <link rel='stylesheet' type='text/css' href='https://www.haobala.com/wenda/resource/css/index.css' /> <link rel='stylesheet' type='text/css' href='https://www.haobala.com/wenda/resource/css/login.css'/> <link rel='shortcut icon' href='https://www.haobala.com/wenda/resource/font/cake.ico' /> <script src='https://www.haobala.com/wenda/resource/require.js' data-main = 'resource/main' defer async='true'></script></head><body ontouchstart> <!--login section--> <p style='height: auto;'><p class='beforeLogin'> <header><p class='head-bg'></p><p class='touxiang'> <img src='https://www.haobala.com/wenda/img/head/0.png' /></p> </header> <!--登录--> <section id='login'><form action='login.php' method='post' id='loginForm'> <input type='number' value='' placeholder='请输入手机号' name='user' maxlength='11' /> <input type='password' value='' placeholder='请输入密码(6-16位)' name='psw' maxlength='16' /> <button type='submit' id='loginBut'>登录</button></form><span>没有账号?<a href='https://www.haobala.com/wenda/4035.html#regis'>注册</a></span><br /><span><a href='https://www.haobala.com/wenda/4035.html#forget'>忘记密码?</a></span> </section> <!--注册--> <section id='regis'><form id='checkphone'> <input type='hidden' name='forwhat' value='forcheck' /> <input type='number' value='' placeholder='请输入手机号' name='user' maxlength='11' /> <button type='button' style='color: #fff;background: orange;'>点击发送<br />验证码</button></form><form action='register.php' method='post' id='regisForm'> <input type='hidden' name='forwhat' value='forrig' /> <input type='number' value='' placeholder='请输入验证码' name='validate' /> <input type='password' value='' placeholder='设置密码(6-16位)' name='psw' maxlength='16' /> <input type='password' value='' placeholder='重新输入密码' name='pswcheck' /> <button type='submit' id='regisBut'>注册</button></form><span>已有账号?<a href='https://www.haobala.com/wenda/4035.html#login'>登录</a></span><br /><span><a href='https://www.haobala.com/wenda/4035.html#forget'>忘记密码?</a></span> </section> <!--忘记密码--> <section id='forget'><form id='check4forget'> <input type='hidden' name='forwhat' value='forcheck' /> <input type='number' name='findphone' value='' placeholder='请输入注册的手机号' maxlength='11'/> <button type='button' id='but2'>点击发送<br />验证码</button></form><form action='validatePhone.php' method='post' id='forgetForm'> <input type='hidden' name='forwhat' value='forreset' /> <input type='number' value='' placeholder='请输入验证码' name='validate' /> <input type='password' value='' placeholder='重新设置密码(6-16位)' name='psw-forget' maxlength='16' /> <input type='password' value='' placeholder='重新输入密码' maxlength='16'/> <button type='submit' id='reset'>重置密码</button></form><span>已有账号?<a href='https://www.haobala.com/wenda/4035.html#login'>登录</a></span><br /><span>没有账号?<a href='https://www.haobala.com/wenda/4035.html#regis'>注册</a></span> </section></p><p class='alreadyLogin'> <header><p class='head-bg'> <h3></h3></p><p class='touxiang'> <img src='https://www.haobala.com/wenda/4035.html' /></p> </header> <section><p style='margin-top: 60px;'> <a href='https://www.haobala.com/wenda/4035.html#' class='weui-cell weui-cell_access'><p class='weui-cell__bd'> <p>我的收藏</p></p> </a> <a href='javascript:;' class='weui-cell weui-cell_access kt-exit'><p class='weui-cell__bd'> <p style='color:red;'>退出登录</p></p> </a></p> </section></p> </p><script type='text/javascript'> require([’jquery’,’weui’],function($){ });</script></body></html>

报错信息:javascript - requirejs加载报错问题?

问题解答

回答1:

手动声明依赖

require.config({ baseUrl:’resource/app’, paths:{’jquery’:’../lib/jquery-3.2.0.min’,’zepto’:’../lib/zepto.min’,’css’:’../lib/css.min’,’weui’:’../lib/jquery-weui.min’,’cookie’:’../lib/jquery.cookie’,’swiper’:’../lib/swiper’ }, shim: {weui: {deps: ['jquery']}}});

以及 999.html 中加载 require.js 时不要 defer 和 async

标签: JavaScript
相关文章: