组件计算属性未定义

时间:2018-06-14 21:13:27

标签: javascript vue.js ecmascript-6 vuejs2 vue-component

组件的计算name属性未定义

<template>
  <div class="person">
    <p>{{name}}</p>
  </div>
</template>

<script>
  export default {
    name: 'person',
    data () {
      return {
        firstName: 'John',
        lastName: 'Doe',
      }
    },
    computed: {
      name: () => {
        return `${this.firstName} ${this.lastName}`
      }
    }
  }
</script>

导致 undefined undefined

1 个答案:

答案 0 :(得分:0)

我已经有类似的问题,因为计算机必须有这样的setter和getter:

  computed: {
    fullName: {
      // getter
      get: function () {
       return this.firstName + ' ' + this.lastName
     },
     // setter
     set: function (newValue) {
       var names = newValue.split(' ')
       this.firstName = names[0]
       this.lastName = names[names.length - 1]
     }
   }
}