【问题标题】:Data variable undefined from computed Vue从计算的 Vue 中未定义的数据变量
【发布时间】:2021-10-11 18:13:18
【问题描述】:

我认为我没有正确理解某些东西,或者是明显的疏忽,但我无法在我的 Vue 组件的计算属性中访问从我的 data() 函数返回的值。从下面的代码中,我试图返回一个计算属性 newmessage,但它表示消息未定义,如果我尝试,数据中的其他所有内容也是如此。感谢任何帮助:)

<script>
  export default {
    props:['alertdata'],
    data () {
      return {
        message: 'hello',
        expanded: [],
        singleExpand: false,
        alertHeaders: [
          {
            text: 'Rule Number',
            align: 'start',
            sortable: false,
            value: 'RuleNumber',
          },
          
          { text: 'Date', value: 'DateCreated' },
          { text: 'Amount', value: 'Amount' },

        ],
        alerts: this.alertdata,
        transactions : this.alertdata.detail,     
      }
    },
    computed: {
        newmessage : function() { 
        return message.reverse()
        }
      
    }

  }
  
</script>

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    您需要使用this关键字引用data中定义的属性:

    newmessage : function() { 
      return this.message.reverse()
    }
    

    【讨论】:

      【解决方案2】:

      通常,在 Vue 中的方法、计算属性或生命周期处理程序内部,您将使用 this 来引用附加方法/计算/处理程序的组件。 this 指的是函数当前正在执行的上下文。

      在您的情况下,您需要在消息前添加this

      computed: {
          newmessage : function() { 
          return this.message.reverse()
          }
      

      【讨论】:

      • 当然哈哈 - 我想我已经盯着屏幕太久了!谢谢你的回答。
      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 2019-04-05
      • 2021-12-16
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2018-08-26
      • 2021-06-04
      相关资源
      最近更新 更多