【问题标题】:Data passing issue in Vue, with template tag and computed attributeVue中的数据传递问题,带有模板标签和计算属性
【发布时间】:2020-04-22 07:30:10
【问题描述】:

我正在使用 Vuetify 构建我的项目,当我尝试访问 computed 中的属性时遇到错误。这是我的代码

    <v-stepper
      v-model="currentStep"
      :vertical="true"
      :alt-labels="false"
    >
      <template>
        <template v-for="n in ageStepsArray.length">
          <v-stepper-step
            :key="`${n}-step`"
            :complete="currentStep > ageStepsArray[n-1]"
            :step="ageStepsArray[n-1]"
          >
            {{ageStepsArray[n-1]}} years old
          </v-stepper-step>
          <v-stepper-content
            :key="`${n}-content`"
            :step="ageStepsArray[n-1]"
          >
            <v-list
              flat
            >
              <v-list-item-group
                v-model="settings"
                multiple
              >
                <v-list-item v-for="(item,index) in dreamList" :key="index">
                  <template v-slot:default="{active}">
                    <v-list-item-action class="pt-1">
                      <v-checkbox v-model="active"></v-checkbox>
                    </v-list-item-action>
                    <v-list-item-content>
                      <v-list-item-title>{{item.title}}</v-list-item-title>
                      <v-list-item-subtitle>
//error occurs here     Estimated cost: {{item.cost}} // want to change to getCostList[index].cost here
                        <v-btn icon x-small @click.stop="editValue(item)">
                          <v-icon >mdi-pencil-circle-outline</v-icon>
                        </v-btn>  
                      </v-list-item-subtitle>
                    </v-list-item-content>
                  </template>
                </v-list-item>
              </v-list-item-group>
            </v-list>
            <v-btn
              color="primary"
              @click="nextStep(ageStepsArray[n-1])"
            >
              Continue
            </v-btn>
          </v-stepper-content>
        </template>
      </template>
    </v-stepper>

    /// script
    computed:{
      ...mapGetters({
        getCostList: "listCost/getList",
      }),
    }

错误: 属性或方法“getCostList”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。

我可以成功访问v-stepper之外的getCostList,并且我已经搜索了很多解决方案,它似乎与template标签的范围有关,但我发现关于此类问题的结果很少。

我想知道我是否使用了错误的方式来调用“getCostList”,或者有其他方法可以解决错误。

【问题讨论】:

  • 我注意到你在你的问题中同时称它为getCostListgetCostlist。如果计算的名称不同,则在模板中使用错误的其中一个会引发相同的错误...
  • 那是我的错,我再次检查了拼写,他们是getCostList,但仍然有同样的错误。
  • 您可能需要创建minimal, reproducible example 以获得一些帮助。从这段代码中消除所有不必要的东西,包括我们看不到的变量。和/或如果可能的话,在jsfiddle.netcodesandbox.io 上发布问题的复制品

标签: vue.js vuetify.js


【解决方案1】:

你没有提供完整的脚本部分,所以不能说,但这是最基本的答案,我现在可以写。

data() {
  return {
     // Define here
     getCostList: ''; //for Example
  };
},

【讨论】:

  • 不是我,但可能是因为已经有一个名为 getCostList 的计算。声明同名的数据变量是不必要的,并且会引发错误。它也无法解释为什么模板中未定义computed
  • mapGetters 是将存储getter 映射到本地计算属性的助手
猜你喜欢
  • 2019-07-28
  • 2021-07-03
  • 2020-08-02
  • 2022-11-15
  • 1970-01-01
  • 2017-07-11
  • 2021-10-07
  • 2020-10-16
  • 2019-04-24
相关资源
最近更新 更多