mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
Use async/await
This commit is contained in:
parent
a39b9fe13b
commit
eaf362910f
@ -1,8 +1,7 @@
|
||||
{
|
||||
"name": "nuxt-custom-routes",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"axios": "^0.15.2",
|
||||
"axios": "latest",
|
||||
"nuxt": "latest"
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -13,11 +13,9 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export default {
|
||||
asyncData () {
|
||||
return axios.get('https://jsonplaceholder.typicode.com/users')
|
||||
.then((res) => {
|
||||
return { users: res.data }
|
||||
})
|
||||
async asyncData () {
|
||||
const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
|
||||
return { users: data }
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
@ -14,12 +14,13 @@ export default {
|
||||
validate ({ params }) {
|
||||
return !isNaN(+params.id)
|
||||
},
|
||||
asyncData ({ params, error }) {
|
||||
return axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`)
|
||||
.then((res) => res.data)
|
||||
.catch(() => {
|
||||
async asyncData ({ params, error }) {
|
||||
try {
|
||||
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`)
|
||||
return data
|
||||
} catch (e) {
|
||||
error({ message: 'User not found', statusCode: 404 })
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user