首页 文章

道具未被分配到Vue中的data()属性

提问于
浏览
2

我正在创建一个Vue组件,它应根据用户动态选择的过滤器刷新餐馆 .

因此,我必须更新我的Vue组件的 data() 函数中的 filteredRestaurants . 但是,首先,当渲染Vue组件时,它会从 "restaurants" prop获取餐馆信息 .

我试图将 "restaurants" 插入 filteredRestaurants 数据属性以将其设置为默认值 . 不幸的是,商店不会显示高,好像在 filteredRestaurants 被赋予其值后插入 "restaurants" 道具 .

我的问题是,我怎样才能将 "restaurants" 道具变成 filteredRestaurants 以便我以后可以在用户更改过滤器时重新渲染Vue组件 .

<template lang="html">
  <div class="test">
    <Filters></Filters>
  <div>
    <ul class="o-list c-stores">
      <Result v-bind:total="restaurants.length" v-bind:open="isOpen" v-on:toggle="toggleRestaurantList"></Result>
      <li v-for="(restaurant, index) in restaurants" class="c-stores__location" :class="{'first': isFirst(index), 'last': isLast(index, restaurants)}">
        <Location :index="index" :store="restaurant" :link="() => setCurrentRestaurant(restaurant)"></Location>
      </li>
    </ul>
  </div>
  </div>
</template>

<script>
import eventHub from './../../event-hubs/storefinder'
import Location from './Location'
import Filters from './Filters'
import Result from './Result'

export default {
  props: ["restaurants", "isOpen", "currentSearch"],
  data() {
    return {
      attributes : [],
   // Here I am assigning the prop
      filteredRestaurants : this.restaurants
    }
  },
  head: {
    title: function () {
      return {
        inner: this.$t('storefinder.overview')
      }
    },
    meta: function functionName() {
      return [{
          name: 'og:title',
          content: this.$t('storefinder.overview') + ' - ' + this.$t('storefinder.name'),
          id: "og-title"
        },
        {
          name: 'description',
          content: this.$t('storefinder.description'),
          id: "meta-description"
        },
        {
          name: 'og:description',
          content: this.$t('storefinder.description'),
          id: "og-description"
        },
      ]
    }
  },
  components: {
    Location,
    Filters,
    Result
  },
  methods: {
    toggleRestaurantList() {
      eventHub.$emit('showRestaurantList');
    },
    setCurrentRestaurant(restaurant) {
      this.trackRestaurantSelect(restaurant.publicNameSlug);
      this.$router.push({
        name: "store",
        params: {
          restaurant: restaurant.publicNameSlug
        }
      });
    },
    trackRestaurantSelect(restaurantName) {
      dataLayer.push({
        'event': 'GAEvent',
        'eventCategory': 'restaurants',
        'eventAction': 'clickResult',
        'eventLabel': restaurantName,
        'eventValue': undefined,
        'searchTerm': this.currentSearch && this.currentSearch.toLowerCase(),
        'amountSearchResults': 1
      });
    },
    created() {
      eventHub.$on('addFilterTheRestaurants', (attribute) => this.attributes.push(attribute));
      eventHub.$on('removeFilterTheRestaurants', (attribute) => this.attributes = this.attributes.filter(item => item !== attribute));
    },
    isLast: function (idx, list) {
      return idx === list.length - 1;
    },
    isFirst: function (idx) {
      return idx === 0;
    },
  }
}
</script>

唯一的方法是,当我将 filteredRestaurants 作为返回 "restaurants" 的函数时,我在Vue模板中调用它:

filteredRestaurants(){
  return this.restaurants
}

任何帮助赞赏 .

1 回答

  • 1

    如果我理解你的问题,你正在寻找computed property

    computed: {
      filteredRestaurants() {
        return this.restaurants;
      }
    }
    

    只要 this.restaurants 的值发生变化,这将更新 .

相关问题