$(...)。modal()不是nuxt js函数

问题描述 投票:0回答:1

我正在尝试根据路线更改在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) {
    }
  }
}
javascript jquery twitter-bootstrap bootstrap-modal nuxt.js
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.