jquery 不在部署中工作,但在本地 nuxtjs 中工作?

时间:2021-04-01 10:25:59

标签: jquery vue.js nuxt.js

我正在使用 jquery 在我的 nuxt js 应用程序中显示一个弹出窗口并在本地主机和开发环境中工作 nuxt.config.js

script: [
  {
    src: "~plugins/jquery-3.4.1.min.js",
    type: "text/javascript"
  },
],
plugins: [  
  "'~plugins/jquery-3.4.1.min.js"
],

1 个答案:

答案 0 :(得分:0)

不确定为什么要在 Vue/Nuxt 项目中包含 jQuery(这在某种程度上会适得其反),但我想您可以将它包含在 CDN 中,并且它可以在任何环境中完美运行。

script: [
  {
    src: "https://code.jquery.com/jquery-3.3.1.slim.min.js",
    type: "text/javascript"
  }
]

或者这样,更干净一些

yarn add jquery
const webpack = require("webpack");

module.exports = {
  build: {
    vendor: ["jquery"],
    plugins: [
      new webpack.ProvidePlugin({
        $: "jquery"
      })
    ],
  }
};

从此处获取的设置示例:https://kaloraat.com/articles/how-to-use-jquery-in-nuxtjs

你也可以试试这个:https://stackoverflow.com/a/57508835/8816585