Webpack FOSJsRoutingBundle integration with Symfony Flex and Angular

限于喜欢 提交于 2019-12-02 00:37:00

Create file router.js in your assets/js directory. Next, fill router.js with following content:

const routes = require('./fos_js_routes.json');
const router = require('../../vendor/friendsofsymfony/jsroutingbundle/Resources/public/js/router.min.js');
router.setRoutingData(routes);
module.exports = router;

Provide new variable in your webpack.config.js like this:

.autoProvideVariables({
    "Routing": "router",
})

Add new loader to your webpack:

.addLoader({
    test: /jsrouting-bundle\/Resources\/public\/js\/router.min.js$/,
    loader: "exports-loader?router=window.Routing"
})

Finally extend webpack config with:

let config = Encore.getWebpackConfig();

config.resolve.alias = {
'router': __dirname + '/assets/js/router.js',
};

module.exports = config;

It should make Routing a global object so you can use it in other js files like this:

let url = Routing.generate('some_route_name', { param: value });

Hope it helps. Cheers.

Simple fix to get this working:

const routes = require('../../assets/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';

Routing.setRoutingData(routes);
window.Routing = Routing;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!