繁体   English   中英

webpack encore 在 symfony 5 中获取

[英]webpack encore fetch in symfony 5

我正在学习使用 webpack Encore

我想知道是否可以使用'path'函数通过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 > 文章Show.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 中使用“路径”功能?

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

谢谢你的帮助。

谢谢,我成功使用 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

再见

暂无
暂无

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

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