【问题标题】:Vue.js Axios Wordpress callVue.js Axios Wordpress 调用
【发布时间】:2019-04-21 19:33:30
【问题描述】:

谁能帮我解决以下代码的问题? 我想在我的 Vue.js 项目中使用 Axios 从我的 wordpress 安装中获取帖子数据。我收到错误:“...axios 未定义”

博客组件:

<template>
  <section>
    <h1>Blog</h1>

    <ul>
    <li v-for="post of posts" :key="index">
      <p><strong>{{post.title}}</strong></p>
      <p>{{post.body}}</p>
    </li>
  </ul>

  </section>
</template>

<script>
export default {
  data(){
    return {
      posts: []
    }
  },
  created() {
    axios.get(`http://woolff.dk/blog/wp-json/wp/v2/posts`)
    .then(response => {
      this.posts = response.data
    })
  }
}
</script>

main.js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

【问题讨论】:

    标签: wordpress vue.js vuejs2 axios vue-component


    【解决方案1】:

    axios 之前缺少 this 关键字:

    created() {
    this.axios.get(`http://woolff.dk/blog/wp-json/wp/v2/posts`)
    .then(response => {
      this.posts = response.data
    }) 
    

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 2021-07-17
      • 2020-11-07
      • 2018-11-25
      • 1970-01-01
      • 2018-05-21
      • 2021-01-18
      • 2018-03-11
      • 1970-01-01
      相关资源
      最近更新 更多