分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

seajs使用记

发布时间:2023-09-06 02:22责任编辑:沈小雨关键词:js

1、seajs使用外部依赖(第三方工具库,譬如jQuery)

         错误做法:

define(function(require, exports, module){ ??????// ?require可以下载外部依赖,但是他不能将其解析为seajs自身的模块,所以不能这样使用。 ???var $ = require(‘../../depency/3rd_lib/jquery.js‘); ???$(‘#hi‘).html("aaaaaa"); ??});

        正确做法1:

              

index.html<html> ????<head> ???????<title>sea demo</title> ???????<!-- <script type="text/javascript" src="../depency/3rd_lib/jquery.js"></script> --> ???????<script type="text/javascript" src="../depency/3rd_lib/sea-debug.js"></script> ???</head><body><div id="hi"></div> ???<script type="text/javascript"> ???seajs.use(‘../static/test/seaUtil.js‘, function(){ ????????seajs.use(‘../static/test/seajsDemo.js‘); ???????});</script> ???</body></html>seaUtil.js ???????????define(function(require, exports, module){ ???????/** ????* ?require可以下载外部依赖,但是他不能将其解析为seajs自身的模块,所以不能这样使用。 ????* ?外部依赖引入推荐做法: ????* ????1、将外部依赖单独放到一个seajs文件模块中引入,在首页seajs.use的callback里use其他模块,在callback中引入其他初始模块可以防止依赖未引入时使用 ????* ????2、将外部依赖放在首页HTML中加载,在seajs文件模块中直接使用 ????*/ ????????require(‘../../depency/3rd_lib/jquery.js‘); ???// $(‘#hi‘).html("aaaaaa"); ??});seajsDemo.js ????define(function(require, exports, modeule){ ???var seaUtil = require(‘./seaUtil‘); ???eBase = window.eBase = window.eBase || {}; ????????app = window.app = window.app || {}; ????????????$.extend(eBase, { ???????sayHello: function(){ ???????????alert(‘hello , gray boy @‘); ???????} ???}); ???app.userManager = app.userManager || {}; ???}); ???????????????????

     正确做法2:

          

index.html ????<html><head><title>sea demo</title><script type="text/javascript" src="../depency/3rd_lib/jquery.js"></script><script type="text/javascript" src="../depency/3rd_lib/sea-debug.js"></script></head><body><div id="hi"></div> ???<script type="text/javascript"> ???seajs.use(‘../static/test/seaUtil.js‘, function(){ ????????seajs.use(‘../static/test/seajsDemo.js‘); ???????});</script> ???</body></html>seajsDemo.jsdefine(function(require, exports, modeule){ ???var seaUtil = require(‘./seaUtil‘); ???eBase = window.eBase = window.eBase || {}; ????????app = window.app = window.app || {}; ????????????$.extend(eBase, { ???????sayHello: function(){ ???????????alert(‘hello , gray boy @‘); ???????} ???}); ???app.userManager = app.userManager || {}; ???});

seajs使用记

原文地址:https://www.cnblogs.com/hengwu/p/9956916.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved