ero*_*onn 3 routes defined symfony webpack-encore
我尝试在我的 Symfony 4 项目中将 FosJsRouting 与 Webpack-encore 一起使用。
我做了:
1.
composer require friendsofsymfony/jsrouting-bundle
Run Code Online (Sandbox Code Playgroud)
2.
php bin/console assets:install --symlink public
Run Code Online (Sandbox Code Playgroud)
3.
php bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json
Run Code Online (Sandbox Code Playgroud)
在我的app.js中:
// FosJsRouting
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);
Run Code Online (Sandbox Code Playgroud)
现在,如果在app.js中执行 a,console.log(Routing);我会在控制台中获取该对象。
另一方面,不可能在我的模板中使用它。
我有以下错误:
未捕获的引用错误:未定义路由
我不明白,因为我的其他包工作得很好,但路由不行
小智 7
您可能已经找到了解决方案,但以下是其他人的一些信息:
因此,在app.js您导入的地方Routing,它只能在这个特定文件中使用,并且您必须将其导入到您想要的任何地方。但是,您每次都需要设置路线:
应用程序.js
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);
console.log(Routing.generate("exposed_route_name")); // will prints in console /path/to/exposed_route
Run Code Online (Sandbox Code Playgroud)
其他.js
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);
console.log(Routing.generate("another_exposed_route_name")); // will prints in console /path/to/another_exposed_route
Run Code Online (Sandbox Code Playgroud)
这不是一个合适的解决方案,因为它太冗余了,没有人愿意每次都重复相同的代码。
Routing变量另一个解决方案是将其设置Routing为全局并在以下文件中访问它:
应用程序.js
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);
// Setting Routing as global there
global.Routing = Routing;
console.log(Routing.generate("exposed_route_name")); // will prints in console /path/to/exposed_route
Run Code Online (Sandbox Code Playgroud)
其他.js
console.log(Routing.generate("another_exposed_route_name")); // will prints in console /path/to/another_exposed_route
Run Code Online (Sandbox Code Playgroud)
模板.html.twig
{{ encore_entry_script_tags('app') }}
{{ encore_entry_script_tags('other') }}
<script>
console.log(Routing.generate("a_third_exposed_route_name")); // will prints in console /path/to/third_exposed_route
</script>
Run Code Online (Sandbox Code Playgroud)
问题是,当包含全球化的文件时,路由将在任何地方可用。它也将在您的 Web 控制台中提供,我认为这不是一件好事,因为每个人都将能够看到您的所有 fos_js_routing 配置。
Routing3.在自己的模块中导出还有一个解决方案,您可以为其创建一个“假”路由模块。在此文件中,您将设置路由对象配置并将其导出:
路由.js
const routes = require("../../public/js/fos_js_routes");
const Routing = require("../../public/bundles/fosjsrouting/js/router"); // do not forget to dump your assets `symfony console assets:install --symlink public`
Routing.setRoutingData(routes);
module.exports = Routing;
Run Code Online (Sandbox Code Playgroud)
然后将其导入到任何文件中以使用它:
其他.js
const Routing = import("./routing");
console.log(Routing.generate("another_exposed_route_name")); // will prints in console /path/to/another_exposed_route
Run Code Online (Sandbox Code Playgroud)
您不需要设置routing.js为 webpack 条目,也不需要将其包含在模板中。但是,如果您直接在树枝模板中编写 JavaScript,我不知道该怎么做。
希望您能找到解决方案。您还可以在 SymfonyCasts 上查看本教程。
| 归档时间: |
|
| 查看次数: |
4790 次 |
| 最近记录: |