如何在刀片模板 Laravel 中使用指令@push

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44674255/
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-09-14 16:11:02  来源:igfitidea点击:

how to use directive @push in blade template laravel

jquerylaraveldomincludeblade

提问by kuka

I need script only on one page. and it should load after jQuery. I tried in index.blade

我只需要一页上的脚本。它应该在 jQuery 之后加载。我在 index.blade 中尝试过

<script type="text/javascript" src="{{ URL::asset ('js/jquery.js') }}"></script>
@push('custom-scripts')
    <script type="text/javascript" src="{{ URL::asset ('js/custom-scripts.js') }}"></script>
@endpush

and then I should use @stack('custom-scripts') in my view?

然后我应该在我看来使用 @stack('custom-scripts') 吗?

回答by Asur

You just need to make that the opposite way, @pushis meant to be in the childview, which is pushing content to the parent @stashdirective.

您只需要以相反的方式进行,@push即在视图中,它将内容推送到父@stash指令。

So your index.blade.php should have a:

所以你的 index.blade.php 应该有:

@stack('custom-scripts')

And your child view:

你的孩子认为:

@push('custom-scripts')
    <script type="text/javascript" src="{{ URL::asset ('js/custom-scripts.js') }}"></script>
@endpush

You can also use @parentdirective with @sectionlike this:

您还可以像这样使用@parent指令@section

//index.blade.php
@yield('scripts')


//child.blade.php
@section('scripts')
    @parent
    <!-- The rest of your scripts -->
@endsection

If you need more info I'd recommend you to check the documentation. Hope this helped you.

如果您需要更多信息,我建议您查看文档。希望这对你有帮助。

回答by Raghu Aryan

I need script only on one page. and it should load after jQuery. You can add dynamic in any page.

我只需要一页上的脚本。它应该在 jQuery 之后加载。您可以在任何页面中添加动态。

@push('custom-scripts')

// Write here your all script.

@endpush

@push('自定义脚本')

// 在这里写下你的所有脚本。

@endpush