update examples for asyncData

This commit is contained in:
Alexandre Chopin 2017-02-28 15:17:49 +01:00
parent 7f925c22fa
commit a4461ff590
18 changed files with 18 additions and 18 deletions

View File

@ -9,7 +9,7 @@
<script> <script>
export default { export default {
data ({ req }, callback) { asyncData ({ req }, callback) {
setTimeout(function () { setTimeout(function () {
// callback(err, data) // callback(err, data)
callback(null, { callback(null, {

View File

@ -11,7 +11,7 @@
import axios from 'axios' import axios from 'axios'
export default { export default {
async data ({ params }) { async asyncData ({ params }) {
// We can use async/await ES6 feature // We can use async/await ES6 feature
let { data } = await axios.get(`https://jsonplaceholder.typicode.com/posts/${params.id}`) let { data } = await axios.get(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
return { post: data } return { post: data }

View File

@ -15,7 +15,7 @@
import axios from 'axios' import axios from 'axios'
export default { export default {
data ({ req, params }) { asyncData ({ req, params }) {
// We can return a Promise instead of calling the callback // We can return a Promise instead of calling the callback
return axios.get('https://jsonplaceholder.typicode.com/posts') return axios.get('https://jsonplaceholder.typicode.com/posts')
.then((res) => { .then((res) => {

View File

@ -8,7 +8,7 @@
<script> <script>
export default { export default {
layout: 'dark', layout: 'dark',
data ({ req }) { asyncData ({ req }) {
return { return {
name: req ? 'server' : 'client' name: req ? 'server' : 'client'
} }

View File

@ -7,7 +7,7 @@
<script> <script>
export default { export default {
data () { asyncData () {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(function () { setTimeout(function () {
resolve({}) resolve({})

View File

@ -7,7 +7,7 @@
<script> <script>
export default { export default {
data () { asyncData () {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(function () { setTimeout(function () {
resolve({ name: 'world' }) resolve({ name: 'world' })

View File

@ -13,7 +13,7 @@
import axios from 'axios' import axios from 'axios'
export default { export default {
data () { asyncData () {
return axios.get('https://jsonplaceholder.typicode.com/users') return axios.get('https://jsonplaceholder.typicode.com/users')
.then((res) => { .then((res) => {
return { users: res.data } return { users: res.data }

View File

@ -14,7 +14,7 @@ export default {
validate ({ params }) { validate ({ params }) {
return !isNaN(+params.id) return !isNaN(+params.id)
}, },
data ({ params, error }) { asyncData ({ params, error }) {
return axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`) return axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`)
.then((res) => res.data) .then((res) => res.data)
.catch(() => { .catch(() => {

View File

@ -1,6 +1,6 @@
<script> <script>
export default { export default {
data ({ req }) { asyncData ({ req }) {
return { return {
name: req ? 'server' : 'client' name: req ? 'server' : 'client'
} }

View File

@ -7,7 +7,7 @@
<script> <script>
export default { export default {
data ({ req }) { asyncData ({ req }) {
return { return {
name: req ? 'server' : 'client' name: req ? 'server' : 'client'
} }

View File

@ -11,7 +11,7 @@
<script> <script>
export default { export default {
data ({ store, route, userAgent }) { asyncData ({ store, route, userAgent }) {
return { return {
userAgent, userAgent,
slugs: [ slugs: [

View File

@ -16,7 +16,7 @@
<script> <script>
export default { export default {
data ({ env }) { asyncData ({ env }) {
return { users: env.users } return { users: env.users }
} }
} }

View File

@ -10,7 +10,7 @@ export default {
validate ({ params }) { validate ({ params }) {
return !isNaN(+params.id) return !isNaN(+params.id)
}, },
data ({ params, env, error }) { asyncData ({ params, env, error }) {
const user = env.users.find((user) => String(user.id) === params.id) const user = env.users.find((user) => String(user.id) === params.id)
if (!user) { if (!user) {
return error({ message: 'User not found', statusCode: 404 }) return error({ message: 'User not found', statusCode: 404 })

View File

@ -9,7 +9,7 @@
import axios from 'axios' import axios from 'axios'
export default { export default {
data () { asyncData () {
return axios.get('https://jsonplaceholder.typicode.com/photos/4').then(res => res.data) return axios.get('https://jsonplaceholder.typicode.com/photos/4').then(res => res.data)
} }
} }

View File

@ -23,7 +23,7 @@ export default {
if (!from) return 'slide-left' if (!from) return 'slide-left'
return +to.query.page < +from.query.page ? 'slide-right' : 'slide-left' return +to.query.page < +from.query.page ? 'slide-right' : 'slide-left'
}, },
data ({ query }) { asyncData ({ query }) {
const page = +query.page || 1 const page = +query.page || 1
return axios.get('https://reqres.in/api/users?page=' + page) return axios.get('https://reqres.in/api/users?page=' + page)
.then((res) => { .then((res) => {

View File

@ -9,7 +9,7 @@
<script> <script>
export default { export default {
data ({ req }) { asyncData ({ req }) {
return { return {
name: req ? 'server' : 'client' name: req ? 'server' : 'client'
} }

View File

@ -7,7 +7,7 @@
<script> <script>
export default { export default {
data ({ req, isServer }) { asyncData ({ req, isServer }) {
return { return {
name: req ? 'server' : 'client' name: req ? 'server' : 'client'
} }

View File

@ -17,7 +17,7 @@
import socket from '~plugins/socket.io.js' import socket from '~plugins/socket.io.js'
export default { export default {
data (context, callback) { asyncData (context, callback) {
socket.emit('last-messages', function (messages) { socket.emit('last-messages', function (messages) {
callback(null, { callback(null, {
messages, messages,