简体   繁体   English

如何在 Vue.js 中使用 jquery 插件

[英]How to use jquery plugins in Vue.js

I am currently rewriting the code to Vue.js and I want to use some Jquery plugins, but I do not quite understand how to do it.我目前正在将代码重写为 Vue.js 并且我想使用一些 Jquery 插件,但我不太明白该怎么做。 For example i want to use plugin scrollbar: http://gromo.github.io/jquery.scrollbar/demo/basic.html#anchor例如我想使用插件滚动条: http://gromo.github.io/jquery.scrollbar/demo/basic.html#anchor

I understand that I have to initialize it something like this:我知道我必须像这样初始化它:

mounted() {
        if (process.client) {
            $(".product-page .product-filter-wr").scrollbar();
        }
    }

But for this to work, I need to insert the script connection in the code:但要使其正常工作,我需要在代码中插入脚本连接:

<script type="text/javascript" src="jquery.scrollbar.js"></script>

Actually, this is the problem.实际上,这就是问题所在。 I am using template syntax, how can I connect a script to a page?我正在使用模板语法,如何将脚本连接到页面? How can I connect the plugin to the page correctly?如何将插件正确连接到页面?

use利用

npm install jquery

after that, you need to import it之后,您需要导入它

import * as $ from 'jquery'

once its done use一旦完成使用

mounted() {
        if (process.client) {
            setTimeout(()=>{
                $(".product-page .product-filter-wr").scrollbar();
             },500);
        }
    }

make sure jquery is present in your component.确保 jquery 存在于您的组件中。

If you only want to include the script at a precise moment of the execution (such as the mounted hook), I advise you to use a dynamic loading library, for example loadjs .如果您只想在执行的精确时刻包含脚本(例如挂载的钩子),我建议您使用动态加载库,例如loadjs

var loadjs = require('loadjs'); // or load it using CDN

// ...

mounted() {
  if (process.client) {
    loadjs(['/jquery.js', '/jquery.scrollbar.js'], 'scrollbar');
    loadjs.ready('scrollbar', () => {
      $(".product-page .product-filter-wr").scrollbar();
    });
  }
}

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

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