Blogger Information
Blog 34
fans 0
comment 0
visits 24462
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS框架 -(一)fetch async await 模块 npm
CY明月归
Original
673 people have browsed it

作业内容:

1. 实例演示fetch api, async,await的使用

  1. // cps :尾部调用, 让用户产生一个错觉, 代码是同步执行,并不异步
  2. // 异步操作:定时器, 文件操作, 网络操作
  3. function createdFile(success, failure) {
  4. success();
  5. failure();
  6. }
  7. function success(msg) {
  8. return "success" + msg;
  9. }
  10. function failure(msg) {
  11. return "error" + msg;
  12. }

  1. //createdFile().then(success).then(json).catch(failure)
  2. fetch("https://jsonplaceholder.typicode.com/todos/1")
  3. .then((response) => response.json())
  4. .then((json) => console.log(json));
  5. url = "http://tptest.com/users.php";
  6. async function getInfo(btn,url) {
  7. response = await fetch(url)
  8. result = await response.json();
  9. render(result, btn)
  10. }
  11. //模块
  12. <script type="module">
  13. // 模块: 就是一个外部的js
  14. // 传统js,引入外部js,共享一个全局空间
  15. // 模块有自己的独立作用域
  16. // 导入模块
  17. // import 成员列表 from 模块路径
  18. import * as m from "./1.js";
  19. console.log(m.email);
  20. console.log(new m.Demo().show());
  21. </script>

2. npm 安装与删除包的常用操作

  1. "axios": "^0.26.1"
  2. 0: 大版本
  3. 26:小版本
  4. 1: 补丁
  5. ^: 仅允许小版本更新
  6. *: 允许大版本更新
  7. ~: 仅允许修复补丁
  8. npm i axios -S: 生产依赖
  9. npm i axios -D:开发依赖
  10. npm root -g: 查看全局模块的路径
  1. Windows PowerShell
  2. PS E:\php19\0413> node 2.js
  3. admin@php.cn
  4. PS E:\php19\0413> npm -v
  5. PS E:\php19\0413> npm init -y
  6. Wrote to E:\php19\0413\package.json:
  7. {
  8. "name": "0413",
  9. "version": "1.0.0",
  10. "description": "",
  11. "scripts": {
  12. "test": "echo \"Error: no test specified\" && exit 1"
  13. },
  14. "keywords": [],
  15. "author": "",
  16. "license": "ISC"
  17. }
  18. PS E:\php19\0413> npm install axios
  19. added 2 packages, and audited 3 packages in 7s
  20. 1 package is looking for funding
  21. run `npm fund` for details
  22. found 0 vulnerabilities
  23. PS E:\php19\0413> npm uninstall axios
  24. removed 2 packages, and audited 1 package in 603ms
  25. found 0 vulnerabilities

3. node中的模块声明,导出与导入

  1. const { site } = require('./m1');
  2. //require导入
  3. http = require('http');
  4. //console.log(http);
  5. mysite = require('./m1.js')
  6. console.log(mysite.site);
  7. console.log(mysite.getSite());
  8. //常用导出
  9. module.exports = {
  10. site: '第19期学习',
  11. getSite() {
  12. return this.site + ' (php.cn)';
  13. },
  14. };
  15. //文件模块
  16. fs = require('fs')
  17. fs.readFile('./test.txt', function (err, data) {
  18. if (err) {
  19. return false
  20. } else {
  21. console.log(String(data));
  22. console.log(data.toString());
  23. }
  24. })
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post