【问题标题】:Show Image EXIF for User with VueJS使用 VueJS 为用户显示图像 EXIF
【发布时间】:2020-05-13 12:52:05
【问题描述】:

我在我的项目中使用 Exif.js 和 VueJS。 但我有问题。 当我想在屏幕上显示图像信息时它不起作用。 但它适用于浏览器控制台。 我如何用

显示信息

标签中的用户? 这是我的代码:

<script>
    const app = new Vue({
      el: '#app',
      data: {
        message: 'Vue exif meta info getter',
        DateImage: "DateTimeDigitized"
      },
      components: {
        'picture-input': PictureInput
      },
      methods: {
        onChange(image) {
          console.log('onChange!')
          if (image) {
            EXIF.getData(this.$refs.pictureInput.file, function () {
              console.log('image info', this)
              console.log('exif data', this.exifdata)
              console.log("date image jadid : " + this.DateImage);
            })
          } else {
            console.log(`it's not image`)
          }
        },
        getEI() {
         var old = console.log;
          var logger = document.getElementById('log');
          console.log = function (message) {
            if (typeof message == 'object') {
              logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
            } else {
              logger.innerHTML += message + '<br />';
            }
          }
        }
      }
    })
  </script>







【问题讨论】:

    标签: vue.js exif-js


    【解决方案1】:

    您对数据和反应性有疑问。这里是来自 Vue 指南的概念。

    "Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive."

    意味着数据应该是一个可重用的函数,您需要声明一个变量来应用 exif 值并能够在屏幕上显示它们的更新

    额外,下次包含你的html部分有时错误会在那里。

    从 vue 开始的一个额外的常见问题是 this 的使用,exif 中的 this 与 vue 中的 this 不一样。绕过“this”问题的一种简单方法是将 vue this 保存在一个临时变量中(让 vm = this)并在 exif 代码中使用它们。

    就这样:

    <template>
      <!-- exifs is an object, you can print direct a value 
           if you know their key (exifs.Name) or you can iterate 
           with v-for and show all the values -->
      <p v-for="ex in exifs">
        {{ex}}
      </p>
    </template>
    
        <script>
            const app = new Vue({
              el: '#app',
              data() {
                return {
                  message: 'Vue exif meta info getter',
                  DateImage: "DateTimeDigitized",
                  exifs: null
                }
              },
              components: {
                'picture-input': PictureInput
              },
              methods: {
                onChange(image) {
                  console.log('onChange!')
                  if (image) {
                    let vm = this
                    EXIF.getData(this.$refs.pictureInput.file, function () {
                      vm.exifs = this
                    })
                  } else {
                    console.log(`it's not image`)
                  }
                },
                getEI() {
                 var old = console.log;
                  var logger = document.getElementById('log');
                  console.log = function (message) {
                    if (typeof message == 'object') {
                      logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
                    } else {
                      logger.innerHTML += message + '<br />';
                    }
                  }
                }
              }
            })
          </script>
    

    Here the example you use from jsfiddle fixed

    【讨论】:

    • 感谢您的回复。但是如果我现在想为示例时间和模型显示 EXIF 的自定义值,我该怎么办?
    • 运行一次我的代码以验证您提到的 exifs 是否存在于图像中。您将在 exif 对象中找到时间和模型的确切名称。之后,您可以删除 v-for 并将它们称为 {{ exifs.time }} 和 {{ exifs.model }}。这样 var exifs 就包含了所有的 exifs。您可以防止在 data() 中声明时间和模型,并且在 onchange 方法期间仅收取这两个费用后,您还可以验证它们是否存在并在不存在时显示错误消息
    猜你喜欢
    • 2018-05-28
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多