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