【问题标题】:How to set value of array object property inside a promise in VueJS?如何在 VueJS 的 Promise 中设置数组对象属性的值?
【发布时间】:2017-03-01 00:06:41
【问题描述】:

我将feathersjsfeathers-client 与VueJS 一起使用,我得到Cannot set property 'employees' of undefined 类型错误。这是我的代码:

<template>
  <ul>
    <li v-for="employee in employees">{{employee.name}}</li>
  </ul>
</template>

<script>
import feathers from 'feathers/client'
import socketio from 'feathers-socketio/client'
import hooks from 'feathers-hooks'
import io from 'socket.io-client'

const socket = io('localhost:3030')
var app = feathers().configure(hooks())
                    .configure(socketio(socket))
                    .configure(authentication({ storage: window.localStorage }));

export default {
 data() {
   return {
     employees: []
   }
 },
 mounted() {
     app.authenticate().then(function(response) {
          app.service('users').find().then(function(users) {
            this.employees = users.data
          })
        }, function(error) {
          console.log(error)
        })
   }
}

</script>

当我运行它时,我得到了

Uncaught (in promise) TypeError: Cannot set property 'employees' of undefined

在控制台中。

我在这里做错了吗?

【问题讨论】:

    标签: javascript html vue.js vuejs2 feathersjs


    【解决方案1】:

    this里面的函数指向那个函数所以

     mounted() {
             var self = this; //add this line
             app.authenticate().then(function(response) {
                  app.service('users').find().then(function(users) {
                    self.employees = users.data //change this to self
                  })
                }, function(error) {
                  console.log(error)
                })
           }
        }
    

    【讨论】:

    • 我怎么没想到?
    • @FewFlyBy ¯\_(ツ)_/¯
    • 我确实想到了这一点,但它让我发疯,我无法让它工作
    猜你喜欢
    • 2020-10-03
    • 2021-07-19
    • 1970-01-01
    • 2022-08-18
    • 2013-06-11
    • 2013-10-03
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多