14、$axios异步加载数据asyncData

安装@nuxtjs/axios

https://axios.nuxtjs.org/setup

npm install @nuxtjs/axios

nuxt.config.js

export default {
  modules: ['@nuxtjs/axios']
}

mock数据

https://mock.mengxuegu.com/
没有账号直接输入账号、密码登录会自动创建,没有修改密码的入口需要保存好账号密码。

使用

https://axios.nuxtjs.org/usage

asyncData

async asyncData({ $axios }) {
  const ip = await $axios.$get('http://icanhazip.com')
  return { ip }
}

methods/created/mounted/etc

methods: {
  async fetchSomething() {
    const ip = await this.$axios.$get('http://icanhazip.com')
    this.ip = ip
  }
}

你可能感兴趣的:(javascript,前端)