代码之家  ›  专栏  ›  技术社区  ›  Valor_ user3379466

LightGallery只能打开一次(Vuejs)

  •  0
  • Valor_ user3379466  · 技术社区  · 6 年前

    我有这个问题,那个LightGallery只能打开一次。每次单击下一个按钮都没有响应。我正在使用LightGallery作为组件。

    在我的父组件中,我有两个按钮,用于打开图像或视频库

    父组件.vue

    <template>
      <div>
        <button class="btn-secondary" @click="showGallery('IMAGE')">
            {{ $t('showImages') }}
        </button>
        <button class="btn-secondary" @click="showGallery('VIDEO')">
            {{ $t('playVideo') }}
        </button>
        <LightGallery
           v-if="galleryVisible" :gallery-items="galleryItems"
           :gallery-type="galleryType" :user-subscription="userSubscription">
        </LightGallery>
      </div>
    <template>
    
    <script>
    import LightGallery from "./subComponents/LightGallery.vue";
    
        export default {
            name: "Location",
            components: {
                LightGallery: LightGallery,
            },
            data() {
                return {
                    // image / video slide show data
                    locationImages: [],
                    locationVideo: [],
                    galleryItems: {},
                    galleryVisible: false,
                    galleryType: 'IMAGE',
                };
            },
            methods: {
                showGallery(type) {
                    this.galleryItems = {};
                    this.galleryVisible = true;
                    this.galleryType = type;
                    if (type === 'IMAGE') {
                        this.galleryItems = this.locationImages;
                    } else if (type === 'VIDEO') {
                        this.galleryItems = this.locationVideo
                    }
                },
                hideGallery() {
                    this.galleryItems = {};
                    this.galleryVisible = false;
                },
        };
    </script>
    

    这是我的孩子(LightGallery)组件

    <template>
        <div id="lightgallery">
        </div>
    </template>
    
    <script>
    
        // Light gallery
        import 'lightgallery.js'
        import 'lightgallery.js/dist/css/lightgallery.css'
        import 'lg-zoom.js'
        import 'lg-autoplay.js'
        import 'lg-fullscreen.js'
        import 'lg-hash.js'
        import 'lg-pager.js'
        import 'lg-share.js'
        import 'lg-thumbnail.js'
        import 'lg-video.js'
        // Light gallery
    
        export default {
            name: "LightGallery",
            props: {
                galleryItems: {
                    type: Array,
                    required: true
                },
                galleryType: {
                    type: String,
                    required: true
                },
                userSubscription: {
                    type: Object,
                    required: false,
                    default: {
                        active: false
                    }
                }
            },
            methods: {
                openGallery() {
                    // prepare images / video to display them in lightGallery
                    let dynamicElements = [];
                    if (this.galleryType === 'IMAGE') {
                        this.galleryItems.forEach(function (value) {
                            dynamicElements.push({
                                src: '/' + value.hd_path,
                                thumb: '/' + value.thumb_path,
                            });
                        });
    
                    }
                    if (this.galleryType === 'VIDEO') {
                        this.galleryItems.forEach(function (value) {
                            dynamicElements.push({
                                src: 'https://vimeo.com/' + value.vimeo_id,
                            });
                        });
                    }
    
                    let lg = document.getElementById('lightgallery');
                    window.lightGallery(lg, {
                        mode: 'lg-slide',
                        download: false,
                        thumbnail: true,
                        dynamic: true,
                        dynamicEl: dynamicElements,
                        autoplayFirstVideo: true,
                        loadVimeoThumbnail: false,
                    });
    
                    lg.addEventListener('onCloseAfter', function (event, index, fromTouch, fromThumb) {
                        lg.data('lightGallery').destroy(true);
                    }, false);
    
                    window.lightGallery(lg);
                },
            },
            mounted() {
                this.openGallery();
            }
        };
    </script>
    

    问题! 在第一个“刷新”页面上,如果我单击按钮“显示图像”或“显示视频”,则重新加载“图库”,它将打开并运行良好。当我关闭Gallery时,我在开发人员工具中看到错误(可能与我的问题完全无关)

    gallery error

    下一步单击按钮“显示图像”或“显示视频”都不起作用。那么,我如何才能正确地销毁LightGallery,以便重新打开它?如果您需要任何其他信息,请让我知道,我将提供。谢谢您!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Shehara    6 年前

    尝试将此行添加到ShowGallery方法中。

            showGallery(type) {
    
                this.galleryVisible = false // <<--- this line
    
                this.galleryItems = {};
                this.galleryVisible = true;
                this.galleryType = type;
                if (type === 'IMAGE') {
                    this.galleryItems = this.locationImages;
                } else if (type === 'VIDEO') {
                    this.galleryItems = this.locationVideo
                }
            },
    

    光廊似乎只工作一次。但以上 代码行 将重新创建“灯光库”组件,每次单击将启动“灯光库”的按钮之一时,都会有一个新的“灯光库”组件实例(与页面加载时完全相同)。