【问题标题】:Vue.js TypeError: Message Cannot read property 'singlePost' of undefinedVue.js TypeError:消息无法读取未定义的属性“singlePost”
【发布时间】:2018-11-14 12:51:31
【问题描述】:

我在 vue.js 中有博客 我想分别预览每个帖子。 我这样做>

export default{
  name: 'post',
  props:['id'],
  data(){
    return {
      singlePost: { }
    }
  },
  created() {
    BlogPostsApi.getPost(this.id)
      .then(response => {
        console.log(response)
        this.singlePost = response.data
      })
  }

在 blogPosts.js 我有这个代码>

import axios from'axios';
export default{
  getPost (id) {
    return axios.get('http://localhost:8000/api/posts/ddc/' + id)
      .then(response => {
        debugger
        return response.data
      })
  }
}

当我调试时,我收到此错误消息无法读取未定义的属性“singlePost”

【问题讨论】:

  • 看不到代码中您实际尝试读取 singlePost的任何地方。错误指向代码的哪一行?
  • 我在 this.singlePost = response.data 行中的 created() 中实际调试时收到此错误
  • “当我在调试时” 到底是什么意思?错误是来自您的代码还是来自您在控制台中输入的内容?
  • 我的代码中有调试器 .then(response => { debugger return response.data }) 我逐行进行

标签: javascript vuejs2 vue-component


【解决方案1】:
 created() {
var self = this
    BlogPostsApi.getPost(this.id)
      .then(response => {
        console.log(response)
        self.singlePost = response.data
      })
  }

问题解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 2020-12-18
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-12-26
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多