文章目录
axios
response { // 返回对象
data: {},
status: 200,
statusText: "OK",
headers: {}, // 响应头
config: {url: "xx", method: "get", headers: {},} // 请求的配置
}
// 全局默认值
axios.defaults.baseURL = '';
axios.defaults.headers.common['Authorization'] = '';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.interceptors.request.use((config) { // request interceptor
return config
}, (error){})
axios.interceptors.response.use((response){ // 2xx返回的都会进来
if (response.config.responseType === 'blob') {
return response
}
if (response.data.ret !== 0) {
}
}, (error){ // 非2xx返回的都会进来
})
// 实例默认值
axios.create({
baseURL: '',
timeout: 1000,
headers: {'X-Gd-Req': ''},
params: {ID: 1}, // url params; get常用!
responseType: "json", // 默认值. blob arraybuffer document text stream
maxContentLength: 22,
onDownloadProgress: function(evt){
},
onUploadProgress: function(evt){
},
})
// 也可用then方式,但不直观! 也可以结构+重命名: const {data:res}
const res = await axios.request(config)
axios.post('/user/12345', {
data: { // 仅适用于 PUT/POST/PATCH
firstName: 'Fred',
lastName: 'Flintstone'
}
}).then(function(response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
}).catch();
axios.all(iterable)
axios.spread(callback)
pubsub-js
npm install pubsub-js
import pubsub from 'pubsub-js'
this.pubid = pubsub.subscribe('topic', (topicName, data)=>{ // called in mounted()
// 注意:必须写成箭头函数,否则this==undefined
})
pubsub.publish('topic', )
pubsub.unsubscribe(this.pubid) // called in beforeDestroy()
localStorage/sessionStorage
sessionStorage: don’t keep data when explorer closed
localStorage.setItem('token', 'Bearer xxxx') // k/v always be treated as a string; object will call toString()
localStorage.setItem('objKey', JSON.stringify(obj))
JSON.parse(localStorage.getItem('objKey')) // parse(null) --> null
localStorage.removeItem('token')
localStorage.clear()