2017-10-13 90 views
0

我在VueJS中有一個探索頁面(見下文),其中包含稱爲切換 - 切片的子組件,這些子組件像網格一樣佈局。我想要做的是當點擊其中一個「toggle-tiles」時,它會應用一個活動類,您可以從下面的代碼中看到,但是我遇到問題的位置。在子組件之間切換活動類VueJS

如果有一個活動類,我希望其餘的活動類被刪除。所以在任何時候只有一個可以激活。目前,他們都設置爲當我點擊時激活。

請參閱下面的代碼。

<template> 
    <div> 
    <section class="explore"> 
     <div class="explore__banner"> 
     <img src="/static/img/placeholder-banner.jpg" alt="" /> 
     </div> 
     <div class="in"> 
     <div class="explore__intro"> 
      <div class="in"> 
      <h2 class="heading heading--uppercase heading--borderbottom">EXPLORE</h2> 
      <p>Once-in-a-lifetime experiences, art exhibitions, culinary adventures and family fun. Stay inspired and discover 
       your own Algarve.</p> 
      </div> 
     </div> 
     <div class="explore__filters"> 
      <div class="in"> 
      <ul class="list list--inline list--uppercase"> 
       <li> 
       <a href="#">all</a> 
       </li> 
       <li> 
       <a href="#">experiences</a> 
       </li> 
       <li> 
       <a href="#">what's on</a> 
       </li> 
       <li> 
       <a href="#">family</a> 
       </li> 
       <li> 
       <a href="#">food</a> 
       </li> 
       <li> 
       <a href="#">hotel amenities</a> 
       </li> 
      </ul> 
      </div> 
     </div> 
     <div class="explore__blocks"> 
      <div class="in"> 
      <toggle-tile @toggle-active="toggleTile" :showtile="isActive"></toggle-tile> 
      <toggle-tile @toggle-active="toggleTile" :showtile="isActive"></toggle-tile> 
      <toggle-tile @toggle-active="toggleTile" :showtile="isActive"></toggle-tile> 
      </div> 
     </div> 
     </div> 
    </section> 
    <room-builder></room-builder> 
    <offer-carousel :offers="offers"></offer-carousel> 
    </div> 
</template> 

<script> 
    import OfferCarousel from '@/components/OfferCarousel' 
    import RoomBuilder from '@/components/RoomBuilder' 
    import ToggleTile from '@/components/ToggleTile' 
    export default { 
    name: "Explore", 
    components: { 
     'offer-carousel': OfferCarousel, 
     'room-builder': RoomBuilder, 
     'toggle-tile': ToggleTile, 
    }, 
    data() { 
     return { 
     isActive: false, 
     offers: [{ 
      image: '/static/img/placeholder-offer.jpg', 
      active: true, 
      captionText: 'SPECIAL OFFER HEADING', 
      buttonText: 'read more', 
      buttonUrl: '#', 
      opacity: 1, 
      id: 1 
      }, 
      { 
      image: '/static/img/placeholder-offer.jpg', 
      active: false, 
      captionText: 'SPECIAL OFFER HEADING', 
      buttonText: 'read more', 
      buttonUrl: '#', 
      opacity: 0, 
      id: 2 
      } 
     ] 
     } 
    }, 
    methods: { 
     toggleTile: function() { 
     console.log("clicked"); 
     } 
    } 
    } 

</script> 


<template> 
    <div class="block" v-on:click="toggleState" v-bind:class="{active: showBlock }"> 

    <div class="in"> 
     <div class="block__image"> 
     <img src="/static/img/block-placeholder.jpg" alt="" /> 
     <div class="block__image--hover"> 
      <img src="/static/img/svg/open.svg" alt="" /> 
     </div> 
     </div> 
     <div class="block__title"> 
     <div class="in"> 
      <h6 class="heading heading--uppercase">EXPERIENCES</h6> 
      <h3>SUMMER TREATMENTS AT CONRAD SPA</h3> 
     </div> 
     </div> 
    </div> 
    <div class="block__panel"> 
     <div class="in"> 
     <div class="panel__close"> 
      <img src="/static/img/svg/cross.svg" alt="" /> 
     </div> 
     <div class="panel__gallery"> 
      <basic-carousel :images="images"></basic-carousel> 
     </div> 
     <div class="panel__content"> 
      <h6 class="heading heading--uppercase">RIA FORMOSA NATURAL PARK</h6> 
      <p>Spend the day with a local clam picker. A real life adventure on a traditional fisherman’s wooden boat, discovering 
      ancient clam picking secrets. You’ll also visit an oyster bank with oyster degustation and local sparkling wine. 
      </p> 
      <h6 class="heading heading--uppercase">WHEN?</h6> 
      <p>Daily, depending on the tide. Check when booking. 
      </p> 
      <h6 class="heading heading--uppercase">HOW LONG WILL IT TAKE? 
      </h6> 
      <p>Approximately 3 hours including transfer time.</p> 
      <h6 class="heading heading--uppercase">HOW MUCH WILL IT COST?</h6> 
      <p>€250,00 for up to 4 guests. €50 per additional guest. Free for children up to 6 years old. Maximum 12 guests. Return 
      transfers to Faro beach: €62.50 for up to four guests. €87.50 for 5-8 guests. Must be booked in advance. 
      </p> 
      <h6 class="heading heading--uppercase">WHAT’S INCLUDED?</h6> 
      <p>Clam picking equipment, waterproof boots (on request, depending on sizes), oyster tasting & sparkling wine. Don’t 
      forget to bring sunscreen, swimming costumes and flip flops. 
      </p> 
     </div> 
     </div> 
    </div> 
    </div> 
</template> 

<script> 
    import BasicCarousel from '@/components/BasicCarousel' 
    export default { 
    name: 'ToggleTile', 
    components: { 
     'basic-carousel': BasicCarousel, 
    }, 
    props: ['showtile'], 
    data() { 
     return { 
     showBlock: false, 
     images: [{ 
      image: '/static/img/meetings-slide.jpg', 
      active: true, 
      opacity: 1, 
      id: 1 
      }, 
      { 
      image: '/static/img/meetings-slide.jpg', 
      active: false, 
      opacity: 0, 
      id: 2 
      } 
     ], 
     } 
    }, 
    methods: { 
     toggleState: function() { 
     this.$emit('toggle-active'); 
     this.showBlock = this.showtile; 
     this.showBlock = true; 
     } 
    } 
    } 

</script> 

感謝

回答

0

這就是Vue公司眼前一亮。爲此,您需要爲您的瓷磚標識,以便您現在知道哪一個是有效的。一些可以區分一塊瓷磚的東西。

我可以看到這個問題的最好方法是。相反,在ExploreisActive: false,這將是最好有activeTile: 1activeTile: 'FooTile'ToggleTile道具(ID或名稱,以此爲榜樣,但可以是任何東西)

而在你ToggleTile組件發出的事件與道具像:

this.$emit('toggle-active', this.name) 
or 
this.$emit('toggle-active', this.id) 

回到Explore變化toggleTile功能

toggleTile: function (nameOrId) { 
    this.activeTile = nameOrId 
} 


<toggle-tile @toggle-active="toggleTile" :showtile="activeTile === 'FooTile'"></toggle-tile> 

<toggle-tile @toggle-active="toggleTile" :showtile="activeTile === 1"></toggle-tile> 

這裏的「魔術」是,只要activeTile改變,道具showTile將改變顯示活動瓷磚並隱藏其他瓷磚。

這是在Vue中開發時常用的思考方式。說出這種情況是真的,而不是在使用JQuery進行點擊或類似時進行更改。