我试图实现这个:
https://v2.vuejs.org/v2/examples/select2.html
虽然我在我的页面中使用了 Vue 组件,但我并没有制作 SPA。
[Vue warn]: Error in mounted hook: "TypeError: $(...).select2 is not a function"
发现于
我收到这个错误
Error in mounted hook: "TypeError: $(...).select2 is not a function"
found in <Select2> at
C:\laragon\www\lara\resources\assets\js\components\ui\Select2.vue
我在 html 文件中包含了 select2 和我的 vuejs:
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script src="{{ asset('js/content_type_blocks.js') }}?{{ str_random(7) }}"></script>
我想知道为什么我会收到 select2 不是函数的错误?
最佳答案
在Select2插件初始化的时候,jquery还没有加载到全局栈中。
提出几种解决方案:
- 只需通过 NPM 添加 Select2
npm install --save select2
然后导入 - 我建议使用更友好的
Selectize.js
此外,还有一个随时可用的 Vue 组件vue2-selectize
。另请查看此性能测试:https://jsperf.com/chosen-select2-selectize - 您可以尝试使用专门为 Vue 编写的类似解决方案:https://github.com/monterail/vue-multiselect
关于jquery - vue2 网站上的以下教程无法让第三方 jquery 插件 select2 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246951/