简体   繁体   English

Vuejs:从外部脚本导入 function

[英]Vuejs: import function from external script

I need to get some functions from external script inside a component.我需要从组件内的外部脚本获取一些功能。 As i found out, the are two ways of adding external scripts like that (ie paypal smart button):正如我发现的那样,有两种添加外部脚本的方法(即 paypal 智能按钮):

first is in the mounted hook首先是在挂钩中

mounted() {
    let paypalBtn = document.createElement('script')
    paypalBtn.setAttribute('src', 'https://www.paypal.com/sdk/js?client-id=some_client_id')
    document.head.appendChild(paypalBtn)
  }

the second one in the "temlpate" section of the component组件“模板”部分中的第二个

<script type="application/javascript" defer src="https://www.paypal.com/sdk/js?client-id=some_client_id"></script>

The question is how should i, for instance, use paypal.Buttons().render('#element_id') from the external script?问题是我应该如何使用外部脚本中的 paypal.Buttons().render('#element_id')? If i do so inside the mounted hook it will through "is not defined" as expected, so what is the import statement i`m missing out?如果我在已安装的钩子内这样做,它将按预期通过“未定义”,那么我遗漏的导入语句是什么?

Finally i got it somehow最后我以某种方式得到了它

You can add external script into your index.html (in my case before closing body tag) and then just use window .paypal.Buttons().render() or whatever function you need in mounted hook of your component.您可以外部脚本添加到您的 index.html (在我的例子中,在关闭 body 标记之前),然后只需使用window .paypal.Buttons().render() 或您在组件的挂载钩子中需要的任何 function。 It is available in window .anyFuncName它在window .anyFuncName 中可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM