简体   繁体   English

如何在 App.js 中导入 TomSelect 以避免浏览器错误?

[英]How to import TomSelect in App.js to avoid browser error?

I'm working on a symfony 5.4 application that uses webpack / encore.我正在开发一个使用 webpack / encore 的 symfony 5.4 应用程序。 I want to use the tomselect javascript plugin.我想使用tomselect javascript 插件。 i installed it with yarn and want to import it somehow in app.js but it doesn't work.我用 yarn 安装了它,想以某种方式在 app.js 中导入它,但它不起作用。

I tryed this ways in app.js:我在 app.js 中尝试了这种方式:

TomSelect = window.TomSelect = require('tom-select');

and

import {TomSelect} from 'tom-select'

and

import TomSelect from "tom-select/dist/js/tom-select.complete.min.js";

and I write this in the html.twig files:我把它写在 html.twig 文件中:

{% block javascripts %}
    {{ parent() }}
    <script>
        new TomSelect('select', {});
    </script>
{% endblock %}

I always get the error in browser's javascript console:我总是在浏览器的 javascript 控制台中收到错误消息:

Uncaught ReferenceError: TomSelect is not defined未捕获的 ReferenceError:未定义 TomSelect

but if I import this way, TomSelect is work:但如果我以这种方式导入,TomSelect 就可以了:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/tom-select/2.0.1/js/tom-select.complete.js"></script>
    <script>
        new TomSelect('select', {});
    </script>

So what should I write in app.js to make tomselect work?那么我应该在 app.js 中写些什么来让 tomselect 工作呢?

Having the same problem.有同样的问题。 You need to defer the initialization of your TomSelect object.您需要推迟 TomSelect 对象的初始化。 Your JS code gets executed before your app.js is loaded.你的 JS 代码在你的 app.js 被加载之前被执行。

Including TomSelect to your app.js bundle should work how you did it:将 TomSelect 包含到您的 app.js 包中应该可以按照您的方式工作:

window.TomSelect = require('tom-select');

Then, wrap your initialization into a DOMContentLoaded event handler:然后,将初始化包装到 DOMContentLoaded 事件处理程序中:

document.addEventListener("DOMContentLoaded", function(event) {
  new TomSelect('select', {});
});

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

相关问题 React-Native:将组件导入App.js时出错 - React-Native : error while import component to App.js 如何在 NodeJS 中将 fetch 导入我的 app.js? - How to import fetch to my app.js in NodeJS? ./src/index.js 尝试导入错误:“./App.js”不包含默认导出(导入为“App”) - ./src/index.js Attempted import error: './App.js' does not contain a default export (imported as 'App') 404错误,尝试添加app.js - 404 Error, trying to add app.js 如何使用react_intl将语言文件数据动态导入到我的APP.js中 - how to dynamically import languages files data into my APP.js using react_intl 如何在 app.js 中导入具有相同名称但位于不同文件夹中的组件? - How do you import components having same name but in different folder in app.js? 无法将反应组件导入我的app.js - Unable to import react component to my app.js AngularJS:如何避免-从app.js调用的广播事件被触发两次? - AngularJS : How to avoid - Broadcast event called from app.js is triggered twice? 无法将我的商店文件导入App.js错误:“商店”应列在项目的依赖项中 - Cannot import my store file into App.js error: 'store' should be listed in the project's dependencies 将vuex存储导入axios和app.js文件 - import vuex store into axios and app.js file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM