6

I Just have a component and I want to add component script code at the bottom of main page cause it make loading page faster. how can I do this?

point: each script must be written on its component file.

1
  • 2
    Please provide some sample code Commented Apr 9, 2021 at 7:32

2 Answers 2

9

you can write this in your parent component @stack('scripts'), and in your child component you can write like this @push('scripts).

your child component code will be like this

@push('scripts')
<script src="example.js"></>
@endpush

it will only push the scripts to the parent component when you load the child component.

Sign up to request clarification or add additional context in comments.

Comments

0

create a blade file where you want to put all your script link. Let I have created a file inside my resource directory and named it like javascripts.blade.php .The file may look like this.

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script>
window._token = '{{ csrf_token() }}';
</script>



@yield('javascript')

notice that I put @yield('javascript') at the bottom. Now you can use it inside <body></body> tag just using @include(javascripts) or if you're script file is inside a folder you can use @include(folderName.javascripts)

2 Comments

so what if when I have 2 or more component in this sort.is there any way that I can write script that related to each component in its component file ?
Yes you can .Here this example is only for common uses script files. You can create individual for any components just you need to include it from your components

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.