一、使用defer
例:
<script src="XXXXXX.js" defer></script>
二、使用promise
例:
get('./mock/list.json').then((res) =>{
return res.json();
}).then((json) => {
this.setState({
list:json.data
})
}).catch((err) => {
console.log(err)
})
三、使用async await
const makeRequest = async () => {
try {
// this parse may fail
const json = await get('./mock/list.json');
const data = await json.json();
_this.setState({
list:data.data
});
} catch (err) {
console.log(err)
}
}
makeRequest()
参考地址:https://blog.fundebug.com/2017/04/04/nodejs-async-await/