简体   繁体   English

在Laravel应用中基于路径的动态负载块

[英]Dynamically Load Chunks Based on Route in a Laravel Application

I'm writing an app using Laravel that requires different javascript on each page. 我正在使用Laravel编写一个需要在每个页面上使用不同javascript的应用程序。 So, I decided to use Webpack to split the code on a page-by-page basis. 因此,我决定使用Webpack逐页拆分代码。 For example I have a one chunk for code relating to mywebsite.com and another chunk for code relating to mywebsite.com/members/new and an app.js for the common modules. 例如,我有一个块用于与mywebsite.com相关的代码,另一个块用于与mywebsite.com/members/new相关的代码,以及一个用于通用模块的app.js

What would be the best way to dynamically require these files based on the url? 基于URL动态要求这些文件的最佳方法是什么?

You can do this with an if statement in your view template like explained here : 你可以像你解释视图模板的一个if语句做到这一点在这里

@if (in_array(Route::currentRouteName(), ['profile', 'register']))
    <script src="example.js"></script>
@endif

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

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