这快把我逼疯了.

I'm assigning the value from an axios response to my vue data like this:

mounted() {
  axios
  .get('/campaigns/new.json')
    .then(response => (
      this.kits = response.data[0].kits,
     )
  )

I can see with vue developer tools that my this.kits has an array of 8 items (correct)

enter image description here

当我之后试着用this.kits或做console.log(this.kits)时,我得到undefined or empty array.

请帮帮忙.谢谢

mounted() {
  axios
  .get('/campaigns/new.json')
    .then(response => (
      this.kits = response.data[0].kits,
      this.kitProducts = response.data[0].kitproducts,
      this.products = response.data[0].products,
      this.boxes = response.data[0].boxes,
      this.categories = response.data[0].categories,
      this.extras = response.data[0].extras,
      this.isCurrentUser = response.data[0].user,
      this.giftpacks = response.data[0].giftpacks
     )
  )
  console.log(this.kits)

console.log(this.kits)将输出:

enter image description here

推荐答案

您的console.log是在Promise请求启动之后立即执行的,您必须将其放在then块的末尾,或者,正如Nicola所说,使用Async AWait

试试这个:

async mounted() {
  const response = await axios.get('/campaigns/new.json')
  this.kits = response.data[0].kits
  this.kitProducts = response.data[0].kitproducts
  this.products = response.data[0].products
  this.boxes = response.data[0].boxes
  this.categories = response.data[0].categories
  this.extras = response.data[0].extras
  this.isCurrentUser = response.data[0].user
  this.giftpacks = response.data[0].giftpacks

  console.log(this.kits)
}

Javascript相关问答推荐

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

将状态向下传递给映射的子元素

使用i18next在React中不重新加载翻译动态数据的问题

为什么这个JS模块在TypeScript中使用默认属性导入?""

setcallback是什么时候放到macrotask队列上的?

编辑文本无响应.onClick(扩展脚本)

对网格项目进行垂直排序不起作用

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

Reaction组件在本应被设置隐藏时仍显示

钛中的onClick事件需要在两次点击之间等待几秒钟

是否可以将Select()和Sample()与Mongoose结合使用?

对具有相似属性的对象数组进行分组,并使用串连的值获得结果

FireBase FiRestore安全规则-嵌套对象的MapDiff

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

需要刷新以查看Mern堆栈应用程序中的更改

如何为两条动态路由创建一个页面?

单击子按钮时将活动切换类添加到父分区

在采购unpept-visalizations.js时遇到问题

Reaction:从子组件调用父组件中的函数

有没有一种方法可以在不定义任何属性的情况下使用CSS转换?