【问题标题】:How to update Vue data to reflect changes in the HTML made by user input如何更新 Vue 数据以反映用户输入对 HTML 所做的更改
【发布时间】:2019-05-06 09:04:52
【问题描述】:

我将 Vue 用于一个小型应用程序,该应用程序在用户填写表单后将一段动态 HTML 复制到用户的剪贴板。一切正常,但执行一次复制方法后,我似乎无法反映 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>

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

【问题讨论】:

    标签: javascript vue.js virtual-dom


    【解决方案1】:

    您已将input 定义为计算值,但它没有引用任何反应性内容,因此它的值永远不会更新。如果你改用一个方法,每次调用它都会被求值,你会得到当前的值。

    【讨论】:

    • 感谢您的指点!我假设每次调用计算值时都会重新分配它,例如通过我的代码中的 copySignature 之类的方法。这似乎违反直觉……我现在正在研究指令,并在找到解决方案后回复。
    • 每次调用方法时都会对其进行评估。现在我想了想,您可能只使用方法而不是计算方法。
    • 这似乎工作@Roy J!如果您将建议添加到您的答案中,我可以继续接受它。谢谢大佬!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多