亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:“v-on 處理程序中的錯誤:“TypeError:this.filter 未定義”

錯誤:“v-on 處理程序中的錯誤:“TypeError:this.filter 未定義”

人到中年有點甜 2022-12-22 14:51:30
我正在嘗試構建一個創建過濾器按鈕的組件,然后通過事件總線發送過濾器對象中的類型屬性,以便在我的應用程序的其他地方處理。但是,當我在數據部分添加對象(過濾器)數組時,單擊按鈕時出現 this.filter is not defined 錯誤。我想將過濾器數組保留在此組件中,因為我還試圖將活動類動態更改為已單擊的任何按鈕。我錯過了與道具有關的東西嗎?當數據和 v-for 在另一個組件上時,為什么我無法顯示按鈕?為了解決這個問題,我一直在問自己這些問題。<template>  <div>    <button      v-for="(filter, index) in filters"      :key="index"      :class="{ active: index === activeItem }"      @click="emitFilter(), selectItem(index)"      :filter="filter"    >      {{ filter.name }}    </button>  </div></template><script>import EventBus from '@/components/EventBus'export default {  props: {    filter: { type: String }  },  data() {    return {      activeItem: 0,      filterResult: '',      filters: [        { name: 'All', type: 'all' },        { name: 'Holidays', type: 'holiday' },        { name: 'Seasons', type: 'season' },        { name: 'Events', type: 'custom' }      ]    }  },  methods: {    emitFilter() {      this.filterResult = this.filter      EventBus.$emit('filter-catagories', this.filterResult)    },    selectItem(index) {      this.activeItem = index    }  }}</script>我的按鈕組件用于過濾器組件<template>  <div>    <span>filters</span>      <FilterBtn />    </div>  </div></template><script>import FilterBtn from '@/components/FilterBtn'export default {  components: {    FilterBtn      }  // data() {  //   return {  //     filters: [  //       { name: 'All', type: 'all' },  //       { name: 'Holidays', type: 'holiday' },  //       { name: 'Seasons', type: 'season' },  //       { name: 'Events', type: 'custom' }  //     ]  //   }  // }}</script>如您所見,注釋部分是我最初擁有過濾器的地方,但我不得不將它們移至按鈕組件以添加活動類。
查看完整描述

4 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

我假設您實際上是在嘗試訪問from within的filter迭代器。為此,您需要在綁定中傳遞itself :v-for="(filter, index) in filters"emitFilter()filter@click


<button v-for="(filter, index) in filters"

        @click="emitFilter(filter)">

然后,您的處理程序可以直接使用參數:


export default {

  methods: {

    emitFilter(filter) {

      this.filterResult = filter

      //...

    }

  }

}


查看完整回答
反對 回復 2022-12-22
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

您正在將一個名為filtertyped的道具傳遞string給您的組件。當您輸出時,{{ filter.name }}您實際上是在引用此屬性,而不是filter您在 v-for 循環中創建的變量。

除非您將名為“filter”的屬性傳遞給您的組件,否則該屬性將是未定義的。因此輸出filter.name將導致此錯誤消息。



查看完整回答
反對 回復 2022-12-22
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

您正在將一個名為filtertyped的道具傳遞string給您的組件。當您輸出時,{{ filter.name }}您實際上是在引用此屬性,而不是filter您在 v-for 循環中創建的變量。

除非您將名為“filter”的屬性傳遞給您的組件,否則該屬性將是未定義的。因此輸出filter.name將導致此錯誤消息。


查看完整回答
反對 回復 2022-12-22
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

是的,你沒有將 prop 傳遞給你的組件,這就是為什么它是未定義的。

<FilterBtn filter="test" />

在這里,我傳遞了一個名為 propfilter的值test。

當然你可以傳遞動態道具。綁定就行了

<FilterBtn :filter="yourData" />

我需要問:你傳遞的是對象還是字符串?

因為您將 prop 定義為字符串,但實際上將其用作對象

  {{ filter.name }}

也許您還應該將類型設置為 Object。

  props: { 
     filter: { type: Object }
  },


查看完整回答
反對 回復 2022-12-22
  • 4 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號