javascript - 在挂载的钩子(Hook)函数中初始化Viewer.js。 (错误 : The first argument is required and must be an element)

标签 javascript vue.js vuejs2 viewer

<分区>

我是 vue.js 的新手,我正在尝试使用 npm 模块实现图像缩放、旋转、缩放和其他一些功能:npm viewerjs 模块。

我遵循了这些步骤:Github repo .我遇到了如下问题:

Issue

研究了 github 问题并在这里找到了答案:Github issue link .@fengyuanchen 说:

have to initialize Viewer.js in the mounted hook function.

如何初始化mounted中的viewer.js?

最佳答案

TommyF 的 react 非常好。但是,我建议将查看器用作动态 Vue 数据,这样您就可以在 Vue 组件中使用查看器库方法、事件及其所有功能。

   const app = new Vue({
      el: '#app',
      data() {
        return {
          viewer: null,
          mode: 'modal',
        }
      },
      created() {

      },
      methods: {
        zoom(value) {
          this.viewer.zoom(value);
        },
        close() {
          this.viewer.exit();
        },
        toggleMode(newmode) {
          if (newmode != this.mode) {
            this.mode = newmode;
            this.viewer.destroy();
            this.viewer = new Viewer(this.$refs.gallery, {
              inline: (this.mode === 'inline'),
              url: 'data-original',
            });
          }
        }
      },
      mounted() {
        this.viewer = new Viewer(this.$refs.gallery, {
          inline: false,
          url: 'data-original',
        });
      }
    })

请看下面的例子:

const app = new Vue({
  el: '#app',
  data() {
    return {
      viewer: null,
      mode: 'modal',
    }
  },
  created() {

  },
  methods: {
    zoom(value) {
      this.viewer.zoom(value);
    },
    close() {
      this.viewer.exit();
    },
    toggleMode(newmode) {
      if (newmode != this.mode) {
        this.mode = newmode;
        this.viewer.destroy();
        this.viewer = new Viewer(this.$refs.gallery, {
          inline: (this.mode === 'inline'),
          url: 'data-original',
        });
      }
    }
  },
  mounted() {
    this.viewer = new Viewer(this.$refs.gallery, {
      inline: false,
      url: 'data-original',
    });
  }
})
<link rel="stylesheet" href="https://fengyuanchen.github.io/viewerjs/css/viewer.css">
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://fengyuanchen.github.io/viewerjs/js/viewer.js"></script>

<style>
  .grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    grid-auto-rows: 1fr;
  }
  
  .grid::before {
    content: '';
    width: 0;
    padding-bottom: 100%;
    grid-row: 1 / 1;
    grid-column: 1 / 1;
  }
  
  .grid>*:first-child {
    grid-row: 1 / 1;
    grid-column: 1 / 1;
  }
  
  .grid img {
    width: 100%;
    height: 100%;
  }
</style>
<div id="app">

  <button @click="zoom(0.1)"> zoom + </button>
  <button @click="zoom(-0.1)"> zoom - </button>
  <button @click="close()"> close </button>
  <button @click="toggleMode('inline')"> inline </button>
  <button @click="toggleMode('modal')"> modal </button>

  <div>
    <div ref="gallery" class="grid">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-1.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-1.jpg" alt="Cuo Na Lake">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-2.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-2.jpg" alt="Tibetan Plateau">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-3.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-3.jpg" alt="Jokhang Temple">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-4.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-4.jpg" alt="Potala Palace 1">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-5.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-5.jpg" alt="Potala Palace 2">
      <img data-original="https://fengyuanchen.github.io/viewerjs/images/tibet-6.jpg" src="https://fengyuanchen.github.io/viewerjs/images/tibet-6.jpg" alt="Potala Palace 3">


    </div>
  </div>
</div>

关于javascript - 在挂载的钩子(Hook)函数中初始化Viewer.js。 (错误 : The first argument is required and must be an element),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53166812/

相关文章:

javascript - Vue 失去 react 性

vue.js - Vue JS - 如何限制输入字段中的特殊字符?

javascript - Angular 2 : Delete method not allowed

javascript - Javascript 或 jQuery 中的脚本或插件的生命周期

typescript - 如何在 Vuex 模块中使用 RxJS

javascript - Vue 同类div,只展开child,没有(this)parent

javascript - 使用 .NET Web 服务开发纯 HTML+JS 应用程序是否可行且安全?

javascript - 无法将 json 转换为 javascript 中谷歌图表所需的格式

javascript - VueJS : clicking disabled button still changes route

vue.js - 仅在移动设备上添加类 - Vuejs 和 bootstrap