javascript - 如何更新 Vue 数据以反射(reflect)用户输入对 HTML 所做的更改

标签 javascript vue.js virtual-dom

我正在将 Vue 用于一个小型应用程序,该应用程序在用户填写表单后将一段动态 HTML 复制到用户的剪贴板。一切正常,但在执行一次复制方法后,我似乎无法反射(reflect) HTML 中的更改。我觉得虚拟 DOM 有一些我不太理解的地方,它阻碍了我找到问题的解决方案。

这是 Vue js:

var app = new Vue({
  delimiters: ['${', '}'], // Alternative delimiters for Twig+Vue
  el: '#app',
  data: {
    lastname: null,
    firstname: null,
    mailto: null,
    occupation: 'Consultant',
    absoluteurl: absBaseUrl,
    companyId: 9, // Default company ID
    companies: []
  },
  computed: {
    logo() {
      return this.absoluteurl + companies[this.companyId].logo;
    },
    setMailto() {
      return 'mailto:' + this.mailto;
    },
    input() {
      return this.$refs.signaturepreview.innerHTML;
    }
    // signatureBanner() {
    //   if (companies[this.companyId].)
    //   return companies[this.companyId].logo;
    // }
  },
  methods: {
    changeComp: function() {
      console.log('company select input changed');
    },
    copyToClipboardFF: function(text) {
      window.prompt ("Copy to clipboard: Ctrl C, Enter", text);
    },
    copySignature: function() {
      // console.log(this.input);
      var success   = true,
          range     = document.createRange(),
          selection;
      // For IE.
      if (window.clipboardData) {
        window.clipboardData.setData("Text", this.input);
      } else {
        // Create a temporary element off screen.
        var tmpElem = $('<div>');
        tmpElem.css({
          position: "absolute",
          left:     "-1000px",
          top:      "-1000px",
        });
        // Add the input value to the temp element.
        tmpElem.text(this.input);
        $("body").append(tmpElem);
        // Select temp element.
        range.selectNodeContents(tmpElem.get(0));
        selection = window.getSelection ();
        selection.removeAllRanges ();
        selection.addRange (range);
        // Lets copy.
        try {
          success = document.execCommand ("copy", false, null);
        }
        catch (e) {
          this.copyToClipboardFF(this.input.val());
        }
        if (success) {
          alert ("Signature is copied to the clipboard!");
          // remove temp element.
          tmpElem.remove();
        }
      }
    }
  },
  mounted() {
  this.companies = companies; // Get json data in the Vue instance
}
})

相关部分是计算数据input(),以及方法copySignature

HTML 看起来像这样:

<div ref="signaturepreview" id="signature-preview" class="preview-wrapper signature-preview">

  <table class="signature" style="
    All sorts of info fetched from a form with jQuery
  </table>
</div>


<button id="validate-signature" class="button button--green" @click="copySignature">Je valide ma signature</button>

当单击按钮时,每次都会刷新输入数据,但它只起作用一次。之后,无论我做什么,剪贴板上的内容都保持不变。

最佳答案

您已将 input 定义为计算值,但它不引用任何反应式内容,因此其值永远不会更新。如果您使用方法,则每次调用时都会对其进行求值,并且您将获得当前值。

关于javascript - 如何更新 Vue 数据以反射(reflect)用户输入对 HTML 所做的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53621389/

相关文章:

javascript - 使用来自另一个库的 javaScript 移动元素后,React 无法更新 DOM

javascript - 如何在加载时提交 MVC View ?

javascript - 如何从对象数组中获取键值列表 - JavaScript

javascript - es6 propertyIsEnumerable

javascript - JQuery 在位置插入表格行

javascript - Vue 哪里导入组件?

vue.js - 如何在vue组件对象中访问 parent 的props值?

javascript - Vue.js - 检测是否断开连接

返回按钮时出现参数错误后的 Javascript:missing )

javascript - Vue和Prismic富文本: add event listener to a span node