【问题标题】:Loading Plaid JS from CDN in VueJS在 VueJS 中从 CDN 加载 Plaid JS
【发布时间】:2022-11-04 18:57:12
【问题描述】:

我正在尝试让 Plaid 在 VueJS 中工作,但我无法加载 JS。

使用 Vanilla JS 可以正常工作: https://jsfiddle.net/5vnh2ubs/

但这似乎不起作用:

<template>
  <div id="app">
    {{ pl }}
  </div>
</template>

<script>
export default {
  beforeMount() {
    this.pl = this.Plaid;
  },
  data() {
    return {
      pl: null,
    };
  },
};
</script>

我尝试在 index.html 文件中导入脚本并使用 vue-plugin-load-script 但似乎没有任何效果。

在 index.html 文件中加载 JS 文件时,我从来没有遇到过这个问题。

编辑:

这就是我尝试使用 vue-plugin-load-script 加载它的方式:

this.$loadScript('https://cdn.plaid.com/link/v2/stable/link-initialize.js')
.then(() => {
    console.log(this.Plaid);
})
.catch(() => {
    console.log('error');
});

在 index.html 中,它是 head 标签之间的一个简单的脚本标签:

<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script>

当我检查元素但调用 Plaid 时,我可以看到脚本标签在那里。

【问题讨论】:

  • 您能否提供您如何在index.html 中导入脚本的代码以及vue-plugin-load-script 的配置。值得一提的是这个模块存在:npmjs.com/package/vue-plaid-link
  • 嗨尼克,已经修改了我原来的帖子。我试过使用那个包,但它导致了它自己的问题。

标签: javascript vue.js plaid


【解决方案1】:

Plaid CDN 脚本创建一个全局变量:window.Plaid。但是,您的组件通过this.Plaid 错误地引用了它。在 Vue SFC 中,this 是组件实例,它只包含在组件定义中声明的自己的属性。 this 不包括来自 window 的全局变量。

解决方案是使用window.Plaid

export default {
  data() {
    return {
      pl: window.Plaid,
    }
  }
}

demo

【讨论】:

  • 谢谢托尼。如此业余的错误。我以前做过,所以应该知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2013-01-23
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
相关资源
最近更新 更多