webpack encore 在 symfony 5 中获取

我正在学习使用 webpack Encore


我想知道是否可以使用“路径”函数通过 ajax 调用路由


webpack.config.js


    .setOutputPath ('public / build /')

    .setPublicPath ('/ build')

    .addEntry ('app', './assets/js/app.js') 

    .addEntry ('ajax', './assets/js/ajax.js')


我创建了第二个条目,它导入所有 ajax 调用


ajax.js


import 'bootstrap'; // adds functions to jQuery


import articleShow './myAjax/articleShow';

articleShow();

...


myAjax > 文章显示.js


是否可以使用路径功能?


        ...

        let formData = new FormData();


        formData.append("folder", folder);

        formData.append("id", id);


        **var urlAjax = "{{ path('showArticle') }}";** (pb)!!!

        //var urlAjax = "https://localhost:8000/article/show";

        fetch(urlAjax, {

            method: 'POST',

            body: formData

        })

            .then(function (response) {

                return response.json();

            })

            .then(function (message) {

            ...


文章控制器


    /**

    * @Route("/article/show", name="showArticle")

    */

    public function showArticle(....) {

     ...


问题 1:是否可以在 webpack encore 中使用 'path' 功能?


问题2:我的做法正确吗?如果没有,您是否有具体示例的建议,因为我几乎找不到任何文档。


感谢您的帮助。


慕婉清6462132
浏览 144回答 1
1回答

回首忆惘然

谢谢我使用 webpack encore 成功了。帮助 :安装 Fos jsrouting 包step 1 :  expose your route in your controller otherwise you have to modify the file: fos_js_routes.json in the public folder or rename the route/*** @Route("/country", **options={"expose"=true},** name="ajaxCountry") **/step 2 : import Routing from '../../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';const routes = require('../../../public/js/fos_js_routes.json');Routing.setRoutingData(routes); var formData = new FormData();    var urlAjax = Routing.generate('yourRoute');    fetch(urlAjax, {        method: 'POST',        body: formData    })        .then(function (response) {            return response.json();        })        .then(function (message) {           ...step 3 : in your terminal  php bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json再见
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript