laravel 将单个 blade 组件连同组件的脚本一起加载到主组件中
laravel load invidual blade component into main component along with script of the component
我有一个包含 3 个组件的文件。例如
<div1></div1>
<div2></div2>
<div3></div3>
@section('script')
<script>
/**
contains div1,div2,div3 dependent script
**/
</script>
现在我为 div
元素
保留了单独的 blade.php
例如,
div1.blade.php
<div1>
</div1>
@section('script')
<script>
// this contain all the script related to div1 like ajax call and other things
</script>
div2.blade.php
<div2>
</div2>
@section('script')
<script>
// this contain all the script related to div2 like ajax call and other things
</script>
div3.blade.php
<div3>
</div3>
@section('script')
<script>
// this contain all the script related to div3 like ajax call and other things
</script>
现在我已将所有这些 blade 组件包含到主要组件中
main.blade.php
@section('content')
@include('div1.blade')
@include('div2.blade')
@include('div3.blade')
@endsection
@section('script')
//common script
@endsection
这些我都已经完成了,但不知道如何在 main 中加载单个组件脚本。blade.php
尝试使用以下方法来实现您的目标。
// main.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@yield('style')
<title>Hello, world!</title>
</head>
<body>
@yield('content')
@yield('script')
</body>
</html>
现在在您的其他文件中扩展上述 main.blade.php 文件。
// other.blade.php
@extend('main')
@section('style')
//your all style files
@endsection
@section('content')
//your page content
@endsection
@section('script')
//your all scripts files
@endsection
希望对您有所帮助。
我有一个包含 3 个组件的文件。例如
<div1></div1>
<div2></div2>
<div3></div3>
@section('script')
<script>
/**
contains div1,div2,div3 dependent script
**/
</script>
现在我为 div
元素
例如,
div1.blade.php
<div1>
</div1>
@section('script')
<script>
// this contain all the script related to div1 like ajax call and other things
</script>
div2.blade.php
<div2>
</div2>
@section('script')
<script>
// this contain all the script related to div2 like ajax call and other things
</script>
div3.blade.php
<div3>
</div3>
@section('script')
<script>
// this contain all the script related to div3 like ajax call and other things
</script>
现在我已将所有这些 blade 组件包含到主要组件中
main.blade.php
@section('content')
@include('div1.blade')
@include('div2.blade')
@include('div3.blade')
@endsection
@section('script')
//common script
@endsection
这些我都已经完成了,但不知道如何在 main 中加载单个组件脚本。blade.php
尝试使用以下方法来实现您的目标。
// main.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@yield('style')
<title>Hello, world!</title>
</head>
<body>
@yield('content')
@yield('script')
</body>
</html>
现在在您的其他文件中扩展上述 main.blade.php 文件。
// other.blade.php
@extend('main')
@section('style')
//your all style files
@endsection
@section('content')
//your page content
@endsection
@section('script')
//your all scripts files
@endsection
希望对您有所帮助。