Uncaught TypeError: $s2.select2 is not a function

Uncaught TypeError: $s2.select2 is not a function

我正在使用 symfony2 并尝试使用 tetranz/select2entity-bundle。

我的layout.html.twig:

'@SBCPlatformBundle/Resources/public/jquery/dist/jquery.min.js'         
            '@SBCPlatformBundle/Resources/public/bootstrap/dist/js/bootstrap.min.js'
            '@TetranzSelect2EntityBundle/Resources/public/js/select2entity.js'

和我的 XXXType(表格):

 ->add('fournisseur', Select2EntityType::class, [
        'multiple' => false,
        'remote_route' => 'fournisseur_home',
        'class' => 'SBCPlatformBundle:fournisseur',
        'primary_key' => 'id',
        'text_property' => 'name',
        'minimum_input_length' => 2,
        'page_limit' => 10,
        'allow_clear' => true,
        'delay' => 250,
        'cache' => true,
        'cache_timeout' => 60000, // if 'cache' is true
        'language' => 'fr',
        'placeholder' => 'Choisir ...',
    ])

所以我遇到了这个错误:

main_select2entity_3.js:10 Uncaught TypeError: $s2.select2 is not a function
at HTMLSelectElement.<anonymous> (http://localhost/SBC/web/js/compiled/main_select2entity_3.js:10:13)
at Function.each (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:2861)
at n.fn.init.each (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:845)
at n.fn.init.$.fn.select2entity (http://localhost/SBC/web/js/compiled/main_select2entity_3.js:2:10)
at HTMLDocument.<anonymous> (http://localhost/SBC/web/js/compiled/main_select2entity_3.js:85:48)
at i (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:27151)
at Object.fireWith [as resolveWith] (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:27914)
at Function.ready (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:29707)
at HTMLDocument.J (http://localhost/SBC/web/js/compiled/main_jquery.min_1.js:2:29892)(anonymous function) @ main_select2entity_3.js:10each @ main_jquery.min_1.js:2each @ main_jquery.min_1.js:2$.fn.select2entity @ main_select2entity_3.js:2(anonymous function) @ main_select2entity_3.js:85i @ main_jquery.min_1.js:2fireWith @ main_jquery.min_1.js:2ready @ main_jquery.min_1.js:2J @ main_jquery.min_1.js:2

所以有人可以帮助解决这个问题吗?

您似乎缺少一些依赖文件:

尝试将 https://github.com/select2/select2/blob/4.0.0/dist/css/select2.css 放入 web/css

并将https://github.com/select2/select2/blob/4.0.0/dist/js/select2.js放入web/js

然后将它们都添加到您的资产脚本中:

'@SBCPlatformBundle/Resources/public/jquery/dist/jquery.min.js'
'@SBCPlatformBundle/Resources/public/bootstrap/dist/js/bootstrap.min.js'
'js/select2.js'
'@TetranzSelect2EntityBundle/Resources/public/js/select2entity.js'

如果需要,还可以在模板中包含它的样式:

<link rel="stylesheet" href="{{ asset('css/select2.css') }}" />

然后 运行 再 php app/console assets:install 看看是否有效