简体   繁体   English

在 nuxt.js 视图中使用 jquery

[英]using jquery in nuxt.js view

I'm trying out nuxt.js for the first time.我是第一次尝试nuxt.js。 But I can't get JQuery to work.但我无法让JQuery工作。

I've installed the npm package alongside with bootstrap.js and popper.js .我已经安装了npm包以及bootstrap.jspopper.js

Also added this to the nuxt.config.js :还将其添加到nuxt.config.js

vendor: ['jquery', 'bootstrap'],
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery'
      })
    ],

But when I try to use it in the view it doesn't work.但是当我尝试在视图中使用它时它不起作用。 First I get:首先我得到:

"$" is not defined “$”未定义

Then when I try to require it by require $ from 'jquery' I get undefined when I try to use $ .然后,当我尝试通过require $ from 'jquery'来要求它时,当我尝试使用$时,我得到了 undefined 。

I'm programming .Net usually so this is not my strongest part.我通常在编程.Net,所以这不是我最擅长的部分。

I'm trying to use $ like so:我正在尝试使用$像这样:

$.each(this.imageList, function(key, image) {
    formData.append(`images[${key}]`, image)
  })

As said it gives me different undefined errors depending if I require it or not.如前所述,它给了我不同的未定义错误,具体取决于我是否需要它。

How can I make it recognize the $ ?我怎样才能让它识别$

I think jquery can be used everywhere in the project.我认为jquery可以在项目的任何地方使用。 so global declaration will be best case.所以全局声明将是最好的情况。 you can simply add JQuery in your project ,你可以简单地在你的项目中添加 JQuery ,

  1. keep your JQuery.js into static folder将您的 JQuery.js 保存到静态文件夹中
  2. just add piece of code into Nuxt.config.js file like below:只需将一段代码添加到 Nuxt.config.js 文件中,如下所示:

script: [{src: 'jquery-3.3.1.min.js'}]脚本:[{src:'jquery-3.3.1.min.js'}]

hope your problem will solve.希望你的问题能解决。

see the image:看图片: 在此处输入图片说明

you can download JQuery from here你可以从这里下载 JQuery

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

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