一.Promise的使用
语法上Promise是一个构造函数,用来封装异步操作并获取其成功或失败的结果
//异步编程解决方案:文件的读取、ajax等等
const fs = require('fs') //引入node.js中的本地文件扩展模块
//实例化Promise
//Promise有三个状态:初始化状态、成功、失败
//resolve函数类型的参数,可以将Promise的状态设置成功
//reject函数类型的参数,可以将Promise的状态设置失败
const p = new Promise((resolve, reject) => {
//执行异步操作
//第一个参数:读取文件的路径
//第二个参数:读取过程中对相应结果的处理
fs.readFile('./他.txt', (err, data) => {
//当文件读取失败时的错误对象
if(err) {
//console.log('文件读取失败')
//return
reject(err) //可以将Promise状态改为失败
}
//当文件读取成功时的文件内容
//onsole.log(data.toString())
resolve(data) //可以将Promise状态改为成功
})
})
//p.then() //当Promise的状态为成功时,then被调用
//p.catch() //当Promise的状态为失败时,catch被调用
p.then((response) => {
//成功业务逻辑
console.log(response.toString())
}).catch((error) => {
//失败业务逻辑
console.log('读取失败\n' + error)
})
二.axios的使用
1.axios安装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<script>
axios({
url: 'http://127.0.0.1:8080/user/list',
method: 'get',
}).then((response) => {
console.log('成功\n', response.data)
}).catch((error) => {
console.log('失败\n', error)
})
</script>
</body>
</html>
2.解决跨域问题
跨域原因 | 示例 |
域名不同 | www.jd.com和www.taobao.com |
域名相同端口号不同 | www.jd.com:8080和www.jd.com:8888 |
二级域名不同 | test1.jd.com和test2.jd.com |
http和https也属于跨域
解决方式
1.在Controller层加@CrossOrigin注解
package com.example.mybatisplus.controller;
import com.example.mybatisplus.entity.User;
import com.example.mybatisplus.service.UserService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/user")
@CrossOrigin //解决跨域问题
public class UserController {
@Resource
private UserService userService;
@GetMapping("/list")
public List<User> list() {
return userService.list();
}
}
另外axios有两种使用方式:方法调用、对象调用
<script>
//方法调用
axios({
url: 'http://127.0.0.1:8080/user/list',
method: 'get',
}).then((response) => {
console.log('成功\n', response.data)
}).catch((error) => {
console.log('失败\n', error)
})
//另一种:对象方式调用
axios
.get('http://127.0.0.1:8080/user/list')
.then((response) => {
console.log('成功\n', response.data)
}).catch((error) => {
console.log('失败\n', error)
})
</script>
3.自定义axios
3.1 配置实例
//使用自定义配置
const request = axios.create({
baseURL: 'http://localhost:8080', //url前缀
timeout: 1000, //请求超时时间
headers: {'token': 'helen123'} //携带令牌
})
使用时:自定义名可代替axios使用,请求url前缀可省略
<script>
//使用自定义配置
const request = axios.create({
baseURL: 'http://localhost:8080', //url前缀
//timeout: 1000, //请求超时时间
//headers: {'token': 'helen123'} //携带令牌
})
//方法调用
request({
url: '/user/list',
method: 'get',
}).then((response) => {
console.log('成功\n', response.data)
}).catch((error) => {
console.log('失败\n', error)
})
//另一种:对象方式调用
request
.get('/user/list')
.then((response) => {
console.log('成功\n', response.data)
}).catch((error) => {
console.log('失败\n', error)
})
</script>
三.拦截器
1.请求拦截
//请求拦截
request.interceptors.request.use(
function(config) {
console.log('config', config)
//config.headers.mytoken = '修改令牌'
//放行
return config
}, function(error) {
return Promise.reject(error)
}
)
2.响应拦截
//响应拦截
request.interceptors.response.use(
function(response) {
console.log('response', response)
//放行
return response.data
},
function(error) {
return response.reject(error)
}
)
四.模块化项目
1.模块化规范
node.js中require引入模块,exports 导出模块
ES6模块化规范:export导出模块,import 引入模块
a.js文件
export let str = '张三' //单个导出
// export {str,...} //一次性导出多个
b.js文件
export let str = '张三'
<!--
模块化解决的问题,javascript程序在全局空间内被污染的问题
-->
<script src="a.js"></script>
<script src="b.js"></script>
<script>
console.log(str)
</script>
<!--解决上面代码导入覆盖问题-->
<script type="module">
import * as a from './a.js'
import * as b from './b.js'
<!--另一种导入模式,一次性导入多个,两个文件内属性重名时需规定别名-->
import {str, stt} from './a.js'
import {str as str1, sst} from './a.js'
</script>
2.默认导入模块
js定义
export default {
name: '李四',
age: 30,
code() {
console.log('hello')
}
}
html应用
<!--导入默认模块-->
<script>
import a1 from './a1.js'
</script>
3.封装多个模块
将多个模块封装到一个模块
m.js
import * as a from './a.js'
import * as b from './b.js'
import a1 from './a1.js'
html应用
<script src="m.js" type="module"></script>