简体   繁体   English

Vue js - 获取未捕获的 ReferenceError:未定义 jQuery

[英]Vue js - Getting Uncaught ReferenceError: jQuery is not defined

I am new to Vue.js and trying to create a custom component that uses jQuery formBuilder plugin.我是 Vue.js 的新手,正在尝试创建一个使用 jQuery formBuilder插件的自定义组件。 When I include the component file inside another component, I am getting an error:当我将组件文件包含在另一个组件中时,出现错误:

Uncaught ReferenceError: jQuery is not defined in /resources/js/form-builder.min.js未捕获的 ReferenceError:jQuery 未在 /resources/js/form-b​​uilder.min.js 中定义

I created a custom component with name formBuilder.vue .我创建了一个名为formBuilder.vue的自定义组件。 Here is the component code:下面是组件代码:

<template>
   <div class="content">
      <formBuilder/>
   </div>
</template>
<script>
   // import './jquery.min.js';
   // import './jquery-ui.min.js';
   // import './form-builder.min.js';
   export default {      
       created() {

       },
       data() {
           return { 

           }
       },
       mounted() {
         jQuery(this.$el).formBuilder();
       },
       methods: {
       }
   }
</script>

In app.js file, which is placed in resource/js/app.js , I am calling this vue to be recursively used by other components:app.js文件中,它位于resource/js/app.js ,我调用这个 vue 以供其他组件递归使用:

window.Vue = require('vue');
require('./bootstrap');
require('admin-lte');
require('./datatable');
import router from './router';
import Datepicker from 'vuejs-datepicker';
import CKEditor from 'ckeditor4-vue';
import FullCalendar from 'vue-full-calendar';
import 'fullcalendar/dist/fullcalendar.css'
import Vue from 'vue';
import jQuery from 'jquery'
import './form-builder.min.js';
Vue.use(VueRouter)
Vue.use(FullCalendar);
Vue.use(CKEditor)
Vue.component("vue-datepicker", Datepicker);
Vue.component('FormBuilder', require('./components/tools/formBuilder.vue').default);

const app = new Vue({
  el: '#app',
  router
});

This is the component file where i am using formbuilder component这是我使用 formbuilder 组件的组件文件

  <template>
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0 text-dark">Questionnaire</h1>
                    </div>
                    <div class="col-sm-6"></div>
                </div>
            </div>
        </div>
        <div class="content">
            <div class="container-fluid">
                <div class="row justify-content-center">
                    <div class="col-md-12">
                        <div class="card">
                            <FormBuilder/> <!--- used formbuilder component --->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        created() {
        },
        data() {
            return {

            }
        },
        methods: {

        }
    }
</script>

I have attached the error我附上了错误截屏 as well.以及。

Can you guys help me find where I am doing wrong?你们能帮我找出我做错的地方吗?

Thanks in Advance.提前致谢。

Importing an object into Vue's app JS doesn't automatically produce that object for use by other components.将对象导入 Vue 的应用程序 JS 不会自动生成该对象以供其他组件使用。

There are at least two ways to do this (though I recommend avoiding all this and just importing jQuery in the components that need it):至少有两种方法可以做到这一点(尽管我建议避免所有这些,只在需要它的组件中导入jQuery ):

Option 1: Vue.prototype选项 1: Vue.prototype

In your app JS, add jQuery to the Vue prototype after you import it, which will make it accessible to every component using the syntax this.jQuery :在您的应用 JS 中,在导入之后将 jQuery 添加到Vue原型中,这将使每个组件都可以使用this.jQuery语法this.jQuery

Vue.prototype.jQuery = jQuery

Option 2: window object选项 2: window对象

Alternatively, you could add it to the window object after importing and use it like window.jQuery :或者,您可以在导入后将其添加到 window 对象并像window.jQuery一样使用它:

window.jQuery = jQuery

Option 3: Individual imports选项 3:个别进口

It's probably more readable/maintainable to simply import it in components that use it:简单地将它导入到使用它的组件中可能更具可读性/可维护性:

import jQuery from 'jquery'

and then you can use it with the syntax in your example.然后您可以将它与示例中的语法一起使用。

That worked for me (Vue 3) for some reason i had to stop the cli and restart由于某种原因,这对我有用(Vue 3)我不得不停止 cli 并重新启动

window.$ = window.jQuery = require('jquery');

Eslint may return "Unexpected top level property" "$". Eslint 可能会返回“意外的顶级属性”“$”。 You will need to insert an exception in your package.json您需要在 package.json 中插入一个异常

"env": {
    ...
    "jquery": true,
 },

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

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