Asset Help (Laravel Blade)

Asked

Viewed 6,930 times

1

all right?

I’m working on a project with the Laravel framework. In my project I can call a js file, but it doesn’t work.

  <script src="{{asset('js/formCurriculo.js')}}" type="text/javascript" async="true" defer></script>

Upon noticing the console I come across this error:

https://i.stack.imgur.com/lZwho.png

Before "public" is coming with 2 bars I think the problem is this, but I do not know how to solve. That’s why I’m here and ask for help for you.

  • Your file is saved with the .blade.php extension?

3 answers

3


if you do not precede the path with a backslash

If you start with a name, your browser will assume that what you want is in the current directory.

You have to think of old school, where everything was based on folders.

So if your url is myhost.com/postse you ask js/scriptname.js, your browser asks myhost.com/js/scriptname.js because you are thinking that you mean that js is in the same folder as the posts.

The problem then comes when you are in the "folder" myhosts.com/posts/my-favourite-post because your browser will try to load, myhost.com/posts/js/scriptname.js because it thinks that js is in the current folder.

on the other hand

If you start your resource path with a bar, ie, /js/scriptname.js, the browser assumes that the js folder is outside the root folder - no matter how many folders you are in the URL

simplifying. just start with your import code bar

<script src="{{ asset('assets/js/formCurriculo.js') }}" type="text/javascript" async="true" defer></script>
  • Friend, thank you very much it worked out, however, I did not understand what happened.

1

Remembering that this may depend on the configuration of your Webpack and Htdocs path. But I believe that this should work :

<script src="{{ asset('assets/js/formCurriculo.js') }}" type="text/javascript" async="true" defer></script>

Take a look later at best practices. https://laravel.com/docs/5.5/mix#vanilla-js

1

The arqivo is safe with the extension .blade.php?

Why do you ask? The printed result should be something like:

<script type="text/javascript" src="nttp://localhost8000/js/algo.js"></script>

In the case of a web server, the domain should appear after the folder js and not the public.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.