博客列表 >0801-xhr和fetch异步编程、模块的导入导出、命名空间

0801-xhr和fetch异步编程、模块的导入导出、命名空间

三九三伏
三九三伏原创
2022年08月22日 19:07:54492浏览

一、xhr与fetch异步编程的步骤,实例演示。

XHR与Fetch的区别:
1.xhr基于传统的ajax。
2.fetch是现代的异步请求,推荐使用,基于Promise对象。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body style="display: grid;gap:2em;place-items: center center;">
  10. <button style="width: 200px;" onclick="getUser1(this)">XHR</button>
  11. <button style="width: 200px;" onclick="getUser2(this)">Fetch</button>
  12. <script>
  13. function getUser1(btn){
  14. // 创建对象
  15. const xhr = new XMLHttpRequest();
  16. // 响应类型
  17. xhr.responseType = "json";
  18. // 配置参数,true表示异步请求,可以省略。
  19. // 不传get参数,默认返回全部用户,用table显示。
  20. let url = "http://website.io/Study/0801/website/users.php";
  21. // 如果有get参数,用户id,用ul列表显示。
  22. // url = 'http://website.io/Study/0801/website/users.php?id=1';
  23. xhr.open("GET", url, true);
  24. // 请求回调
  25. xhr.onload = ()=> {
  26. console.log(xhr.response);
  27. //将数组渲染到页面
  28. render(xhr.response, btn);
  29. }
  30. // 失败回调
  31. xhr.onerror = ()=> console.log("Error!");
  32. // 发起请求
  33. xhr.send(null);
  34. }
  35. function getUser2(btn) {
  36. let url = "http://website.io/Study/0801/website/users.php";
  37. // fetch所有操作是异步的,但通过then的使用,顺序可控。
  38. fetch(url)
  39. .then(function (response){
  40. return response.json();
  41. })
  42. .then(function(json){
  43. // 打印到控制台
  44. console.log(json);
  45. // 页面渲染
  46. render(json, btn);
  47. })
  48. }
  49. </script>
  50. <script src="0801/js/function.js"></script>
  51. </body>
  52. </html>

点击XHR和Fetch获取数据效果:

ECMA2017中简化回调

  1. <script>
  2. // function getUser(btn) {
  3. // // 请求自己的接口
  4. // let url = "http://website.io/Study/0801/website/users.php";
  5. // fetch(url)
  6. // .then(response => response.json())
  7. // .then(json => {
  8. // console.log(json);
  9. // // 前端渲染
  10. // render(json, btn);
  11. // })
  12. //-----------以下非注释部分是简化的代码--------------
  13. // ECMA2017中,有async,await,用来简化异步回调方法
  14. async function getUser(btn) {
  15. // 请求自己的接口
  16. let url = "http://website.io/Study/0801/website/users.php";
  17. // 异步耗时,等待结果
  18. const response = await fetch(url);
  19. // 获取结果,转为json
  20. const result = await response.json();
  21. console.log(result);
  22. // 前端渲染
  23. render(result, btn);
  24. }
  25. </script>

关于跨域

跨域:同源策略,仅允许协议相同,域名相同,端口相同,否则为跨域请求,一般是禁止的。图片和js脚本引用是可以跨域的,js脚本之内通常是不能发起跨域请求的。
// php中允许跨域请求,“”表示任何域名都可以过来跨域。
header(“access-control-allow-origin:
“);

二、模块成员导出与导入,别名使用场景与命名空间演示

1. 模块的导入导出

为什么要用模块?

在js中,所有代码共用一个全局作用域:window,容易冲突。在js6中引入模块,来控制作用域。

作用域有哪些?

全局作用域
函数作用域
块作用域
模块作用域

js模块说明

一个模块就是一个js文档,模块有自己独立的作用域,模块中的成员默认都是私有,到处才能用。

———————模块成员的导出———————————————————
逐个导出
m1.js

  1. // 逐个导出
  2. export let username = '用户1';
  3. export function hello(username) {
  4. return `Hello, ${username}`;
  5. }

test.html对导出的引用

  1. <!-- 默认浏览器是不支持模块的,需要指定type -->
  2. <script type="module">
  3. // 导入模块
  4. import {username, hello} from './m1.js';
  5. console.log(username);
  6. console.log(hello(username));
  7. </script>

统一导出
m1.js

  1. // 统一导出(1)声明(2)导出
  2. // 声明
  3. let username = '用户1';
  4. function hello(username) {
  5. return `Hello, ${username}`;
  6. }
  7. // 导出
  8. export {username, hello};

test.html对导出的引用

  1. <!-- 默认浏览器是不支持模块的,需要指定type -->
  2. <script type="module">
  3. // 导入模块
  4. import {username, hello} from './m1.js';
  5. console.log(username);
  6. console.log(hello(username));
  7. </script>

别名使用场景,在有名字冲突或者想要隐藏真实命名的时候可以使用别名代替

m2.js

  1. // 别名导出(1)声明(2)导出
  2. // 声明
  3. let username = '用户2';
  4. function hello(username) {
  5. return `Hello, ${username}`;
  6. }
  7. // 别名导出
  8. export {username as user, hello as getName};

test.html对导出的引用

  1. <!-- 默认浏览器是不支持模块的,需要指定type -->
  2. <script type="module">
  3. // 导入模块
  4. import {user, getName} from './m2.js';
  5. console.log(user);
  6. console.log(getName(user));
  7. </script>

2.命名空间

导入成员过多时可以挂在一个对象上,这个对象就叫命名空间。

命名空间使用

m3.js

  1. // 默认成员(default表示)的导出,不是声明,导入时需要命名。
  2. let username = '用户3';
  3. function hello(username) {
  4. return `Hello, ${username}`;
  5. }
  6. let useremail = 'user@php.cn';
  7. // 私有成员不导出
  8. let sex = 'male';
  9. // 将useremail作为默认导出
  10. export {username, hello, useremail as default };

test.html对导出的引用

  1. <!-- 默认浏览器是不支持模块的,需要指定type -->
  2. <script type="module">
  3. // 导入模块
  4. import * as user from './m1.js';
  5. console.log(user);
  6. </script>

访问成员

  1. <script type="module">
  2. ...
  3. // 访问成员
  4. console.log(user.username);
  5. console.log(user.hello(user.username));
  6. console.log(user.default);
  7. </script>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议