fetch api的使用
调用JSONPlaceholder网页获取假数据
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(response => response.json())
.then(jsons => console.log(jsons));
输出该网页运行结果一样的数据:completed: false
id: 1
title: "delectus aut autem"
userId: 1
后一个then使用前一个then的封装结果.
text()方法属于fetch API的一部分,返回一个Promise实例对象,用于获取后台返回的数据.
await与async的使用
async function get() {
let response = await fetch("https://jsonplaceholder.typicode.com/users");
// .then(response => response.json())
let returns = await response.json();
// .then(jsons => console.log(jsons));
console.log(returns);
}
get();//打印输出returns
结果为:(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
async:将函数声明为异步函数。
await:只能在异步函数使用,赋值一个不确定的时间。
npm安装
在终端里进入需要的安装的文件夹,执行安装指令:npm init -y
npm删除包
npm i axios:安装包指令
npm uni axios:删除包指令
node模块的导出与导入
导出:
daoru.js:
exports.er = "新手1314";
exports.getEr = function () {
return this.ex + "(php.cn)";
};
zuoye.js:
let myphp = require("./daoru.js");
console.log(myphp);
console.log(myphp.getEr());
在终端运行zuoye.js,分别输出:
{ er: '新手1314', getEr: [Function (anonymous)] },
新手1314(php.cn)
导入:
muban.js:
let email = "新手1314@php.cn";
function hello(email) {
return "My email is : " + email;
}
let user = { x: 1, y: 2 };
class Demo {
show() {
return "Hello php.cn";
}
}
export { email, hello, user, Demo };//导出的成员
daochu.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>导出模板</title>
</head>
<body>
<script type="module">
import * as php from "./muban.js";
console.log(php.email);
</script>
</body>
</html>
结果:打印 新手1314@php.cn