简体   繁体   English

使用 WebPack Encore 和 Symfony 4 在 TWIG 页面上使用功能 Javascript

[英]Use function Javascript on TWIG page using WebPack Encore and Symfony 4

i'm using Symfony 4.2 and i use last WebPack Encore.我使用的是 Symfony 4.2,我使用的是最后一个 WebPack Encore。

I have issu because i need to use function from my javascript on my twig template, but this never work.我有问题,因为我需要在我的树枝模板上使用我的 javascript 中的函数,但这永远行不通。

I always have same problem:我总是有同样的问题:

Uncaught ReferenceError: consoleText is not defined at (index):17未捕获的 ReferenceError:consoleText 未在 (index):17 处定义

assets/js/app.js:资产/js/app.js:

require('../css/app.css');
const $ = require('jquery');
global.$ = global.jQuery = $;

assets/js/myFunctions.js资产/js/myFunctions.js

const consoleText = function (log = "Auncun log") {
console.log(log);
};

module.exports = {
    consoleText: consoleText
};

webpack.config.js webpack.config.js

var Encore = require('@symfony/webpack-encore');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/js/app.js')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

I need to get access to my function consoleText() outside my file.我需要在我的文件之外访问我的函数 consoleText()。

If i want to get access on javascript file, this work.如果我想访问 javascript 文件,这个工作。 Exemple: In my app.js i can add:示例:在我的 app.js 中,我可以添加:

let myTest = require('./myFunctions');
myTest.consoleText('Blablabla');

this work fine after compilation.编译后可以正常工作。

BUT i need to get access this function on my twig template (home.html.twig)但是我需要在我的树枝模板(home.html.twig)上访问这个功能

In my base.html.twig i called my script file with:在我的 base.html.twig 中,我使用以下命令调用了我的脚本文件:

{% block javascripts %}
    {{ encore_entry_script_tags('app') }}


    // NEED HERE USE MY FUNCTION LIKE:
    {{ encore_entry_script_tags('myFunctions') }}
    <script>
        consoleText("This don't work");
    </script>
{% endblock %}

my consoleTest() function don't work and i don't understand why.我的 consoleTest() 函数不起作用,我不明白为什么。 I try to use Module.export, and tried to use lot of thinks but i don't know why i can't call my function on my twig template, so i need your help.我尝试使用 Module.export,并尝试使用很多想法,但我不知道为什么我不能在我的树枝模板上调用我的函数,所以我需要你的帮助。

Thank you谢谢

@Eöras Think reverse... Pass twig to js instead of using js on twig to pass twig variables. @Eöras 反向思考......将树枝传递给 js 而不是在树枝上使用 js 来传递树枝变量。

https://symfony.com/doc/current/frontend/encore/server-data.html https://symfony.com/doc/current/frontend/encore/server-data.html

example: https://gist.github.com/MkLHX/d4d38f4fd1fe702fdb9b2a0077ca9c81示例: https : //gist.github.com/MkLHX/d4d38f4fd1fe702fdb9b2a0077ca9c81

It's not working and it's as expected, because part of webpack job is to encapsulate the script content so you can't access it globally, letting you only call it once you have imported it.它不工作,并且正如预期的那样,因为 webpack 工作的一部分是封装脚本内容,因此您无法全局访问它,让您只能在导入后调用它。 A way around that could be :一种解决方法可能是:

window.consoleText = function (log = "Auncun log") {
console.log(log);
};

but it would require your script to be imported by any other script before the inline script is running但在内联脚本运行之前,它需要任何其他脚本导入您的脚本

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

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