根据本教程,我尝试在Symfony中使用javascript路由:https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/usage.html
这是我的代码:
...
"render": function (data, type, row, meta) {
return Routing.generate('get-file', { fileId: data });
}
...
修改脚本:
<script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
<script src="{{ path('get-file', { callback: 'fos.Router.setData' }) }}"></script>
<script>
const routes = require('../../public/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
Routing.setRoutingData(routes);
Routing.generate('rep_log_list');
</script>
但是我得到了错误:
[呈现模板时抛出了异常(“缺少必需的参数(“ fileId”)来生成用于路由“获取文件”。”)。
您不需要捆绑包即可。使用默认参数生成路由,然后将其替换为JavaScript变量。
let route = "{{ path('get-file', { filedId: 'fileId_' }) }}";
route = route.replace('fileId_', yourVariable);