Help with Asset (Laravel Blade)

1

Alright?

I'm working on a project with the Laravel framework. In my project I can call a js file, but it does not work.

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

While repairing the console, I come across this error:

Before"public" is coming with 2 bars I think the problem is this, but I do not know how to solve it. So I'm here and I'm asking you for help.

    
asked by anonymous 10.01.2018 / 19:46

3 answers

2

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 about 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're thinking that you mean that js is in the same folder as the posts.

The problem then comes when you're in the myhosts.com/posts/my-favourite-post folder because your browser will attempt to load myhost.com/posts/js/scriptname.js because it thinks js is in the current folder.

on the other hand

If you start your resource path with a slash, that is /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 slash your import code

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

Remembering this may depend on your Webpack configuration and Htdocs path. But I think this should work:

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

Take a look at best practices later. link

    
10.01.2018 / 19:58
1

Is the file saved with the extension .blade.php ?

Why the question? The printed output should look 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 .

    
10.01.2018 / 20:24