简体   繁体   English

根据页面加载不同的js文件

[英]Load different js files depending on the page

I'm trying to optimize the load of all pages on laravel, so I want to load js files only on the pages that use them. 我正在尝试优化laravel上所有页面的加载,因此我只想在使用它们的页面上加载js文件。

I ended up with a commons js file for all and 3 other js for specific files. 最后我得到了所有文件的公共js文件,以及特定文件的其他3个js文件。 Like so: 像这样:

mix   //.scriptsIn('resources/assets/js');
    .scripts([
      // commons
          // cdn
          // 'bootstrap.min.js',
          // 'jquery.min.js',
          'scripts.js',
          'template.js',
      ]);

mix.version(["css/all.css", "js/all.js"]);

elixir(function(mix) {
    mix.scripts([
// welcome page
            'wow.js',
            'jquery.lazyloadxt.extra.min.js',
            // jquery.easing.min.js  // unused
          ], "public/js/welcome.js", 'resources/assets/js');
    mix.version(["js/welcome.js"]);

    mix.scripts([
// chart pages
            'highcharts.js',
            'jquery.highchartTable-min.js',
            ], "public/js/charts.js", 'resources/assets/js');
    mix.version(["js/charts.js"]);

    mix.scripts([
//table pages
            'jquery.tablesorter.min.js',
          ], "public/js/tables.js", 'resources/assets/js');
    mix.version(["js/tables.js"]);

});

So I put my all.js on the master view, and what I'd like to do is a way to include the other on just the pages that I want. 因此,我将all.js放在了主视图上,而我想做的就是将其他文件仅包含在我想要的页面上。 tables.js only on pages that have a table, and so on. 仅在具有表的页面上使用tables.js,依此类推。


Can I do this without having to manually put the script tag on the individual views? 我可以不必手动将script标签放在各个视图上吗?

I'm thinking something like setup a service provider and check for a variable? 我在想类似设置服务提供商并检查变量的事情?

public function boot()
{
    // view()->composer('master', function ($view) {
    //     $view->with('js_include', 'table.js');
    // });
}

And in master view, if exists, include? 在主视图中,如果存在,包括吗? A page could have table and charts, so I would have to do a foreach. 一个页面可以有表格和图表,所以我必须做一个foreach。 Am I overcomplicating it? 我是否过于复杂?

You could write a custom directive for each of the JS scripts and include the created tag name in the templates. 您可以为每个JS脚本编写一个自定义指令,并将创建的标记名称包括在模板中。

class BladeServiceProvider extends ServiceProvider {
    public function boot()
    {
        Blade::directive('chartsjs', function($expression) {
            return "<?php
               echo '<script type=...>...</script>';
        });     
   } 

   //...
}

// view template: example.blade.php

@section('js')
     @chartsjs
@stop

Ok I'm doing: 好吧,我在做:

public function boot()
{
    $welcome = ['home'];
    $charts = ['meal.*'];
    $tables = ['meal.*', 'trainings.*', 'plans.*', 'nutrition.index',
          'history.index', 'exercises.index'];

    $pages = array_merge($welcome, $charts, $tables);

    foreach ($pages as $key => $page) {
      $js_includes = [];
      foreach (['welcome', 'charts', 'tables'] as $key => $value) {
        if (in_array($page, $$value)) $js_includes[] = "js/{$value}.js";
      }

      View::composer($page, function ($view) use ($js_includes) {
          $view->with('js_include', $js_includes);
      });
    }
}

In just the master view: 仅在主视图中:

@if (isset($js_include))
  @foreach($js_include as $key => $js)
    <script src="/{{ $js }}"></script>    
  @endforeach
@endif

It works on localhost but not on heroku deploy :/ 它在localhost上有效,但在heroku deploy上无效:/

I changed view()->composer for View::composer and added use View; 我更改了View::composer view()->composer View::composer并添加了use View; but same issue 但同样的问题


Commenting //protected $defer = true; 注释//protected $defer = true; solved the issue 解决了这个问题

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

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