簡體   English   中英

如何通過引用獲取變量的值?

[英]How can I get a value of a variable by reference?

我有一個名為標簽的數組,其中包含餐廳的名稱。 我想在 GMapMarker 到 go 的 for 循環中使用它來從具有名稱的數組中檢索數據。

let tags[] = {name: 'mcdonalds', id: '1'}, {name: 'burger king', id: '2'}, {name: 'subway', id: '3'}

mcdonalds: [{icon: 'mdi-mcdonalds', position: '2323, 4234'}, {icon: 'mdi-mcdonalds', position: '77654, 34554'} ]
 
burgerking: [{icon: 'mdi-burgerking', position: '756656, 43243'}, {icon: 'mdi-burgerking', position: '8744, 36774'} ]

subway: [{icon: 'mdi-subway', position: '2154, 65654'}, {icon: 'mdi-subway', position: '6453, 3562'} ]

       <div v-for="tag in tags.name" :key="tag">
            <GmapMarker
              v-for="(restaurant, index) in **tag**"
              :key="index"
              :position="restaurant.position"
              :icon="restaurant.icon"
              @click="openMenu(restaurant)"
            />
          </div>

我嘗試使用${{restaurant}} 但它似乎沒有從數組中檢索數據。

這是使用您的數據的示例。 tagsrestaurants通過計算屬性暴露給模板。

筆記:

  • 我把“burger king”改成了“burgerking”,這樣比賽就可以了。
  • 我將示例模板更改為使用列表而不是GmapMarker ,以便它可以在我的環境中運行。
<template>
  <div>
    <div v-for="tag in tags" :key="tag.id">
      <ul>
        <li
          v-for="(restaurant, index) in restaurants[tag.name]"
          :key="index"
          :position="restaurant.position"
          :icon="restaurant.icon"
          @click="openMenu(restaurant)"
        >
          {{ restaurant.position }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    tags() {
      return [
        { name: 'mcdonalds', id: '1' },
        { name: 'burgerking', id: '2' },
        { name: 'subway', id: '3' },
      ];
    },
    restaurants() {
      return {
        mcdonalds: [
          { icon: 'mdi-mcdonalds', position: '2323, 4234' },
          { icon: 'mdi-mcdonalds', position: '77654, 34554' },
        ],

        burgerking: [
          { icon: 'mdi-burgerking', position: '756656, 43243' },
          { icon: 'mdi-burgerking', position: '8744, 36774' },
        ],

        subway: [
          { icon: 'mdi-subway', position: '2154, 65654' },
          { icon: 'mdi-subway', position: '6453, 3562' },
        ],
      };
    },
  },
};
</script>

如果您的計算值是從 Vuex state 讀取的,您的腳本部分可能類似於:

<script>
import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState(['tags', 'restaurants']),
  },
};
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM