简体   繁体   English

Symfony2:如何在另一个js中包含一个js文件?

[英]Symfony2: How to include a js file inside another js?

I am using intl-tel-input plugin in a Symfony project. 我在Symfony项目中使用intl-tel-input插件。 We currently combine Assets in order to load all js script files into one. 我们目前将Assets组合在一起,以便将所有js脚本文件加载到一个文件中。

However the intl-tel-input plugin requires utils.js in order to enable formatting/validation etc. and it needs to be included inside the script, like this: 但是,intl-tel-input插件需要utils.js才能启用格式设置/验证等功能,并且需要将其包含在脚本中,如下所示:

utilsScript: "/Resources/js/vendors/utils.js"

However if I use that I get a 404 error (file utils.js not found). 但是,如果我使用它,则会收到404错误(找不到文件utils.js)。

How can I load that file on the script? 如何在脚本上加载该文件?

This is my complete script: 这是我完整的脚本:

$(".js-phone").intlTelInput({
    autoFormat: false,
    autoHideDialCode: false,
    autoPlaceholder: true,
    defaultCountry: "auto",
    geoIpLookup: function(callback) {
      $.get('http://ipinfo.io', function() {}, "jsonp").always(function(resp) {
        var countryCode = (resp && resp.country) ? resp.country : "";
        callback(countryCode);
      });2
    },
    nationalMode: false,
    utilsScript: "{{ include('/Resources/js/vendors/utils.js') }}" // utils.js enables formatting/validation etc. 
});

According to documentation you provided utilsScript requires path to js file. 根据您提供的文档, utilsScript需要js文件的路径。 You can't just use /Resources/js/vendors/utils.js since Resources is not a public directory. 您不能只使用/Resources/js/vendors/utils.js因为Resources不是公共目录。

Twig's include is meant to INCLUDE file contents, so what you need to use is asset function: Twig的include旨在包含文件内容,因此您需要使用的是asset功能:

$(".js-phone").intlTelInput({
    (..)
    utilsScript: "{{ asset('bundles/yourbundle/js/vendors/utils.js') }}" 
});

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

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