【问题标题】:How can i pass data from window method to the data function in a Vuejs app如何将数据从窗口方法传递到 Vuejs 应用程序中的数据函数
【发布时间】:2020-07-04 23:13:10
【问题描述】:

我需要将数据从 window 方法传递到 vuejs 组件的 data 函数

这是我的窗口函数

window.authenticate = function(pid, receiptKey) {
  console.log("Authentication");
  console.log(this)
  localStorage.setItem("pid",pid)
  alert("pid="+pid+"receipt="+receiptKey)
  window.pid=pid
  window.receiptKey = receiptKey
}

数据函数:

 data: () => ({
    pid: 0,
    receipt: 0
  }),

试图设置 Pid 和接收密钥挂载,我错在哪里?

mounted: function (){

 this.pid = window.pid
 alert(this.pid)
 this.receipt = window.receiptKey

}

【问题讨论】:

  • 究竟是什么问题?
  • 我在上面的窗口方法(window.authenticacte)中从外星人 apk 获取 pid 和receiptKey 现在我必须在我的vuejs 应用程序中使用pid 和receiptkey,但不能分配pid 的值和 vuejs 应用程序内的接收键,它在窗口范围内而不是在应用程序中。

标签: javascript typescript vue.js vuejs2


【解决方案1】:

我做了一些研究,发现创建的生命周期钩子可以用于此目的,如下所示

1)我们需要在下面的例子中将window方法和组件方法window.authenticate绑定到loginCall,这样就可以了。

created: function () {
        // `this` points to the vm instance
        console.log('a is: ' + this.a)
        // window.somefunc = this.greet.bind(this);
        window.authenticate = this.loginCall.bind(this)
    },


  methods: {

  loginCall: function (pid, receipt) {
}

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 2019-01-31
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多