【问题标题】:Evaluate computed property inside a function评估函数内部的计算属性
【发布时间】:2021-10-21 18:03:28
【问题描述】:

我正在 .vue 文件的脚本部分尝试这个:

<template>...    
<script>
    
    const checkType = (value) => {
      // Need to know transferType value
      console.log(this.transferType)

      if (!value) {
        return false
      }
    
      return true
    }
    
    export default {
     data: () => ({/*some data*/}),
     computed: {
      transferType() {
       if (this.$store.state.transfer.type === 'one way') return true
       return false
      }
     }
    }
</script>

如何在 checkType 函数中评估计算属性 transferType 的值?

我将不胜感激任何帮助

【问题讨论】:

  • 是否存在 checkType 不是 vue 实例上的计算函数的原因?
  • 或作为mixin添加
  • transferType() {return checkType(this.$store.state.transfer.type)}
  • @maembe 是的,我使用 checkType 进行自定义 vuelidate 验证,所以我需要知道传输类型才能要求返回日期

标签: vue.js vuejs2


【解决方案1】:

将 store 导入到您的脚本中,并在那里而不是从您的组件实例中访问它。

import store from './store'

const checkType () {
      return store.state.transfer.type === 'one way'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 2018-10-05
    • 1970-01-01
    • 2019-10-22
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多