如何在我的组件VUE 2(webpack)中加载外部js文件?

问题描述 投票:3回答:2

我已经看了,我没找到。我有一个来自外部js文件的URL,我正在我的index.html中导入。应用程序启动时正在加载文件。

但是,用户不会经常使用这些功能;我想仅在调用特定组件时才上传此文件。

我正在使用带有模板webpack的vue cli(vue 2)。

我尝试过类似的东西:

import * as PagSeguroDirectPayment from 'https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js';

required('https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js');

他们不工作。

需要上传此文件才能使用支付网关。有谁能够帮我?

webpack vuejs2 vue-loader
2个回答
1
投票

而不是在index.html中使用它,创建一个单独的js文件并从链接复制脚本。在js文件中导出所需的方法,并将其导入所需的组件中。


0
投票

这是vue所具有的功能之一,让人相信它经过深思熟虑。做这样的事实上是非常容易的。

我在我的路线中使用这个:

// home gets loaded always
const Home = import('@/pages/home')

// profile and account gets loaded on demand
const Profile = () => import('@/pages/profile')
const Account = () => import('@/pages/account')

与webpack一起使用时,这将为每个组件生成单独的文件。 (01.js,02.js等)这些文件将根据需要加载。

© www.soinside.com 2019 - 2024. All rights reserved.