vue data赋值赋不上去

export default {
  name: 'SidebarMenu',
  props: {item: {}},
  data () {
    return {
      menuList: [{}]
    }
  },
  created: function () {
    this.getMenuList()
  },
  methods: {

    getMenuList (event) {
      $.getJSON('/cost-manager/sys/menu/user?_' + $.now(), function (r) {
        // console.log(r)
        this.menuList = r.menuList

      })
    }
  }
}


*
menuList始终为空,赋值赋不上去,求解

*

阅读 6.7k
3 个回答

在getjson外声明 var _self = this; _self.menuList = r.menu.List;

This binding rule:

  1. Default Binding

  2. Implicit Binding

  3. Explicit Binding

  4. new Binding

强烈建议对this不了解的看下YDKJS这篇文章

这里 this 指向不对,应该使用箭头函数:

$.getJSON('xx', (res) => {
    this.menuList
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题