ASCII码 ASCII码

异步请求、 npm 与 node模块的学习

发布于:2022-04-15 13:37:54  栏目:技术文档

一、异步请求的学习

(1)fetch函数

  1. fetch('http://xhr.test.com/users.php')
  2. .then(response => response.json())
  3. .then(json => console.log(json))

(2)async与await 简化fetch

  1. // async 与 await 简化fetch
  2. async function getUser(){
  3. let url = 'http://xhr.test.com/users.php';
  4. const response = await fetch(url);
  5. const result = await response.json();
  6. console.log(result);
  7. }

二、npm 常用操作

(1)初始化项目

  1. npm init -y

(2)npm安装axios包

  1. npm i axios

(3)npm删除axios包

  1. npm uni axios

三、node模块

(1)模块的声明

  1. let email = '123456@php.cn';
  2. function hello(email) {
  3. return 'My email is : ' + email;
  4. }
  5. let user = { x: 1, y: 2 };
  6. class Demo {
  7. show() {
  8. return 'Hello php.cn';
  9. }
  10. }

(2)模块的导出

  1. export { email, hello, user, Demo };

(3)模块的导入

  1. <script type="module">
  2. // 导入模板
  3. import * as myData from './module.js';
  4. // 当前空间前缀: myData
  5. console.log(myData);
  6. console.log(myData.email);
  7. console.log(myData.hello(myData.email));
  8. console.log(myData.user);
  9. console.log((new myData.Demo()).show());
  10. </script>

相关推荐
阅读 +