2017-04-18 68 views
1

嘗試關閉我的模態時遇到了奇怪的行爲。我寫的方法來顯示和關閉模式無法關閉VueJS2中的模態

methods: { 
showModal: function() { 
    this.show = true; 
    console.log(this.show); 
}, 
closeModal: function() { 
    this.show = false; 
    console.log(this.show); 
}, 

莫代爾應通過點擊表

<tr class="hover-hand" @click="showModal"> 

<div v-show="show" class="modal is-active"> 

我試圖用一個單獨的組件來呈現它的行出現,但暫時把一切都在一個如果我試圖打開一個模式一切都是好的截圖模式https://pp.userapi.com/c638017/v638017273/39134/98lcFy5OWvc.jpg

但是,如果我試圖關閉我有一些麻煩的截圖與控制檯輸出它記錄console.log(this.show);兩次用不同的參數。此外,模態不會關閉。 https://pp.userapi.com/c638017/v638017273/39148/mIpSQMQYLNg.jpg

對不起,我不知道如何在創建的jsfiddle項目的簡化版本,在這裏粘貼整個組件代碼

<template> 
 
    <tr class="hover-hand" @click="showModal"> 
 
    <td>{{ beer.name }}</td> 
 
    <td>{{ beer.home }}</td> 
 
    <td>{{ beer.sort }}</td> 
 
    <td>{{ beer.density }}</td> 
 
    <td>{{ beer.alcohol_content }}</td> 
 
    <td>{{ beer.ibu }}</td> 
 
    <td> 
 
     <span class="icon"> 
 
     <i v-if="beer.on_tap === true" v-bind:style="{ color: activeColor }" class="fa fa-check" aria-hidden="true"></i> 
 
     </span> 
 
    </td> 
 
     <div v-show="show" class="modal is-active"> 
 
     <div class="modal-background"></div> 
 
     <div class="modal-content"> 
 
      <div class="card"> 
 
      <div class="card-content"> 
 
       <p class="title"> 
 
       {{ beer.name }} 
 
       </p> 
 
       <p class="subtitle"> 
 
       Jeff Atwood 
 
       </p> 
 
      </div> 
 
      <footer class="card-footer"> 
 
       <p class="card-footer-item"> 
 
       <span> 
 
        View on <a href="https://twitter.com/codinghorror/status/506010907021828096">Twitter</a> 
 
       </span> 
 
       </p> 
 
       <p class="card-footer-item"> 
 
       <span> 
 
        Share on <a href="#">Facebook</a> 
 
       </span> 
 
       </p> 
 
      </footer> 
 
      </div> 
 
     </div> 
 
     <button class="modal-close" @click="closeModal"></button> 
 
     </div> 
 
    </tr> 
 
    
 
</template> 
 

 
<script> 
 
import SingleBeerModal from '@/components/Beerlist/SingleBeerModal'; 
 

 
export default { 
 
    name: 'SingleBeer', 
 
    data() { 
 
    return { 
 
     activeColor: 'green', 
 
     show: false, 
 
    }; 
 
    }, 
 
    methods: { 
 
    showModal: function() { 
 
     this.show = true; 
 
     console.log(this.show); 
 
    }, 
 
    closeModal: function() { 
 
     this.show = false; 
 
     console.log(this.show); 
 
    }, 
 
    }, 
 
    components: { 
 
    'beer-modal': SingleBeerModal, 
 
    }, 
 
    props: ['beer'], 
 
}; 
 

 
</script> 
 

 
<style> 
 
</style>

謝謝!

+0

你嘗試'V系列:click.prevent =「closeModal」'? – Pradeepb

+0

我想你試圖以標準的方式將組件放置到表中... https://vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats –

+0

@Pradeepb是的,它沒有影響 –

回答

1

看起來你有一切之上的@單擊一個TR,所以當你點擊按鈕

<button class="modal-close" @click="closeModal"></button> 

這個點擊也被傳播到主TR,所以Pradeepb它幾乎擁有了,我想你應該嘗試

v-on:click.stop="closeModal" 

這將導致單擊停止傳播到TR,因爲現在正在封閉模式,並在同一時間打開模式。

此外,不要使用點擊TR事件,請嘗試使用的div來reestructurate,上的onclick事件TR可能會導致怪異的行爲

+0

非常感謝!這就是我所需要的'v-on:click.stop =「closeModal」'我也會嘗試重新組織我的代碼。 –