javascript 如何在 vue 组件中包含外部 js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46039148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:24:37  来源:igfitidea点击:

How to include external js inside vue component

javascriptvuejs2vue-component

提问by Bertho Joris

I want to make a slide imagein my app using jQuery Slide Image I get from internet. This source must include external JSand CSS.

我想slide image使用从互联网上获取的 jQuery 幻灯片图像在我的应用程序中创建一个。此源必须包含外部JSCSS

I just want to use this external library only for certain components, so I just want to import it in only one particular component (Home Component).

我只想将这个外部库仅用于某些组件,所以我只想将它导入到一个特定组件 ( Home Component) 中。

My app uses webpackas a module bundler. How do I import external js file? I have tried doing it inside <script>in Vue Components like

我的应用程序webpack用作模块捆绑器。如何导入外部js文件?我试过<script>在 Vue Components里面做这件事,比如

var slide = require('assets/template/js/jssor.slider-26.1.5.min.js');

But I got error :

但我有错误:

Error: Cannot find module "assets/template/js/jssor.slider-26.1.5.min.js"

My application structure enter image description here

我的应用程序结构 在此处输入图片说明

If the way I imported js filesis wrong, how is the right way to use this js only on home component, without all components hit if i register in index.html

如果我导入的方式js files错误的,那么正确的方法是如何只在 home 组件上使用这个 js,如果我在 index.html 中注册,所有组件都不会命中

回答by riyaz-ali

Module paths are resolved relative to the file being compiled. In this case, to import external script you need a path like:

模块路径相对于正在编译的文件进行解析。在这种情况下,要导入外部脚本,您需要一个类似的路径:

var slide = require('../assets/template/js/jssor.slider-26.1.5.min.js');