Heim  >  Fragen und Antworten  >  Hauptteil

Schritte zum Installieren von jQuery in Nuxt.js

Obwohl ich versucht habe, jQuery zu meinem Projekt hinzuzufügen, ist eine Fehlermeldung aufgetreten, die besagt, dass es nicht definiert ist.

plugins: [
    { src: '~/plugins/js/svgSprite.js', mode: 'client' },
    { src: '~/plugins/vendor/jquery/jquery.min.js', mode: 'client' },
    { src: '~/plugins/vendor/bootstrap/js/bootstrap.bundle.min.js', mode: 'client' },
    { src: '~/plugins/vendor/bootstrap-select/js/bootstrap-select.min.js', mode: 'client' }, <-- 给我报错
    { src: '~/plugins/vendor/magnific-popup/jquery.magnific-popup.min.js', mode: 'client' },
    { src: '~/plugins/vendor/smooth-scroll/smooth-scroll.polyfills.min.js', mode: 'client' },
    { src: '~/plugins/vendor/object-fit-images/ofi.min.js', mode: 'client' },
    { src: '~/plugins/js/theme.js', mode: 'client' }
  ],

Warum passiert das? Offensichtlich habe ich jQuery vor Bootstrap-Select deklariert.

P粉755863750P粉755863750347 Tage vor645

Antworte allen(1)Ich werde antworten

  • P粉277824378

    P粉2778243782023-11-07 10:37:21

    我不建议在Nuxt项目中添加jQuery。


    但是如果你真的想要添加,可以按照以下步骤进行:

    • 使用yarn add jquery安装它
    • 将以下内容添加到你的nuxt.config.js文件中
    import webpack from 'webpack'
    
    export default {
      // ...
      build: {
        plugins: [
          new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
          })
        ]
      },
    }
    
    • 确保你的.eslintrc.js文件中有以下内容
    module.exports = {
      // ...
      env: {
        // ...
        commonjs: true,
        jquery: true
      },
    }
    

    然后,你可以在method或类似的地方这样使用它

    $("h2").addClass("tasty-class")
    

    Antwort
    0
  • StornierenAntwort