Javascript 在 Laravel 5.3 中添加 css 和 js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39788403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:54:20  来源:igfitidea点击:

adding css and js file in laravel 5.3

javascriptphpcsslaravel

提问by user3386779

I want to include a css all css and js in single page and load it in all page.Now If I want to include bootstrap.css and bootstrap.js in welcome page I have included in welcome.blade.php page and if i want to add in another page I have included in second.blade.php page also.I want to include it in master page

我想在单个页面中包含一个 css 所有 css 和 js 并在所有页面中加载它。现在如果我想在欢迎页面中包含 bootstrap.css 和 bootstrap.js 我已经包含在welcome.blade.php 页面中,如果我想要添加另一个页面,我已经包含在 second.blade.php 页面中。我想将它包含在母版页中

<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">

回答by Komal

Crate header.blade.php

板条箱 header.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <meta name="description" content="">
    <meta name="author" content="">

    <title>dhinchakwale</title>
    <!-- Bootstrap Core CSS -->
    <link href="{{ asset('/css/bootstrap.min.css') }}" rel="stylesheet">

    <link href="{{ asset('/css/datepicker.css') }}" rel="stylesheet" type="text/css">
    <link href="{{ asset('/css/bootstrap-timepicker.min.css') }}" rel="stylesheet" type="text/css">

    <link href="{{ asset('/css/dataTables.bootstrap.css') }}" rel="stylesheet">    
    <!-- Custom CSS -->  
    <link href="{{ asset('/css/admin.css') }}" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- jQuery -->
    <script src="{{ asset('/js/jquery.min.js') }}"></script>    
  </head>

  <body id="page-top">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->        
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="/"><img alt="Brand" src="{{asset('/images/logo.png')}}" class="img-brand"></a>
        </div>      
      </div>
    </nav>
    <div class="content-div">
      @yield('content')
    </div>
    <div class="clearfix"></div>  

    <!-- Bootstrap Core JavaScript -->
    <script src="{{ asset('/js/bootstrap.min.js') }}"></script> 
    <script src="{{ asset('/js/bootstrap-datepicker.js') }}"></script>
    <script src="{{ asset('/js/bootstrap-timepicker.min.js') }}"></script>
  </body>
</html>

And use like this First extends header Second Section of your content

并像这样使用 First extends header Second Section 您的内容

@extends('layouts.header')
@section('content')
  <section class="content-header">
    <h1>
      Hello
    </h1>
  </section>
@stop  

回答by Amit Kumar

Here is the steps you can follow

以下是您可以遵循的步骤

  1. Create a master template layout
  2. Extend the master template in all pages and paste your link into header section
  1. 创建主模板布局
  2. 在所有页面中扩展主模板并将链接粘贴到标题部分

To create a master template //master.blade.php

创建主模板 //master.blade.php

<html lang="en">

      @yield('header')

<body>
      @yield('page-body')
</body>
</html>

Now Extend this layout in second.blade.php

现在在 second.blade.php 中扩展这个布局

@extend('master.blade')
@section('header')
   <link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">
@endsection
@section('page-body')
   {{!-- content of body --}}
@endsection

回答by Mayank Pandeyz

Define a common layout and include all .js and .css file on that layout and than bind your page with this layout.

定义一个通用布局并在该布局上包含所有 .js 和 .css 文件,然后将您的页面与此布局绑定。

Layout:

布局:

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

Where: @yield directive is used to display the contents of a given section.

其中:@yield 指令用于显示给定部分的内容。

View:

看法:

@extends('layouts.default')
@section('content')
    i am the home page
@endsection

回答by aceraven777

You can define a master layout view file then define your CSS and JS there. Then all of your view file will extend that layout file. See documentation here: https://laravel.com/docs/5.3/blade

你可以定义一个主布局视图文件,然后在那里定义你的 CSS 和 JS。然后您的所有视图文件都将扩展该布局文件。请参阅此处的文档:https: //laravel.com/docs/5.3/blade