【问题标题】:$(...).modal() is not a function nuxt js$(...).modal() 不是函数 nuxt js
【发布时间】:2020-04-06 16:40:06
【问题描述】:

我正在尝试根据路由更改在 vue 组件中切换模式。 当调用 $("#loginModal").modal("show"); nuxt 表明 .modal 不是函数。

我已经在 nuxt.config.js 文件中导入了 jQuery 和 bootstrap.js,并且顺序正确。

我已经尝试从 node_modules 文件夹和 CDN 中引用 bootstrap.js 文件,所以我确信包含 lib 不会出错。

模态组件

<template>
    <div class="modal fade" id="loginModal">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Test modal</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data () {return {

        }}, 
        watch: {
            $route (to, from) {
                if (to.query.modal == "login") {
                    $("#loginModal").modal("show");
                } else {
                    $('#loginModal').modal("hide");
                }
            }
        }
    }
</script>

nuxt.config.js

module.exports = {
  mode: 'universal',
  /*
  ** Headers of the page
  */
  head: {
    title: process.env.npm_package_name || 'website name ;)',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ],
    script: [
      {
        src: "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js",
        type: "text/javascript"
      },

      {
        src: "https://kit.fontawesome.com/a772a37d1d.js", 
        type: "text/javascript"
      },

      {
        src: "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js", 
        type: "text/javascript" 
      }
    ]
  },
  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },
  /*
  ** Global CSS
  */
  css: [
    "~/assets/css/main.css"
  ],
 
  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    
  ],
  /*
  ** Nuxt.js dev-modules
  */
  buildModules: [
    
  ],
  /*
  ** Nuxt.js modules
  */
  modules: [
    '@nuxtjs/axios',
    // "@nuxtjs/dotenv",
  ],
  /*
  ** Axios module configuration
  ** See https://axios.nuxtjs.org/options
  */
  axios: {
  },
  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    extend (config, ctx) {
    }
  }
}

【问题讨论】:

  • 你真的不应该将 jquery 与 nuxt 一起使用。改用 vue-bootstrap

标签: javascript jquery twitter-bootstrap bootstrap-modal nuxt.js


【解决方案1】:

我建议检查以下链接以了解此问题
https://codesandbox.io/s/733xo5v390

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2019-06-14
    • 2021-06-08
    • 2015-11-26
    • 2020-09-10
    • 2021-12-25
    • 2020-04-11
    相关资源
    最近更新 更多