首次使用Vue.js,如何导入Resources目录下的app.js至partials scripts
assets() Pointing to Public Directory Hey there! Let's work through this Vue.js setup problem step by step—we'll get your app.js imported into your partials scripts without changing the assets() method's public directory setting, and fix that annoying "multi" error you hit.
Here's how to resolve it:
Keep your source file in Resources, compile it to Public
Since you can't modify whereassets()points, the cleanest approach is to leave your originalapp.jsinResources/Assets/js/and use a build tool to compile it into thepublicdirectory (whereassets()looks for files). If you're using a tool like Laravel Mix (super common for setups with theassets()helper), update yourwebpack.mix.jswith this line:mix.js('Resources/Assets/js/app.js', 'public/js');Run
npm run dev(for development) ornpm run prod(for production) to compile the file. This will generate a bundledapp.jsinpublic/js/that theassets()method can easily access.Import the compiled file in your partials
Now in your partials' scripts section, use theasset()method to link to the compiled file:<script src="{{ asset('js/app.js') }}"></script>This works perfectly because
assets()targets the public directory, and we've just placed a usable copy of your Vue code there.Why moving files caused the "multi" error
That error typically happens when your build tool (like webpack) detects conflicting entry points or duplicate file paths in the public directory. Manually moving yourapp.jslikely created a conflict with existing build configurations. Keeping source files inResourcesand compiling topublicavoids this mess entirely.If you aren't using a build tool
If you want to skip the build step, manually copyapp.jsfromResources/Assets/js/topublic/js/—make sure the filename is unique if there's already anapp.jsthere (maybe name itvue-app.js). Then import it like:<script src="{{ asset('js/vue-app.js') }}"></script>Just remember to re-copy the file every time you make changes to the original in
Resources.
That should get your Vue.js setup running smoothly without breaking the assets() configuration!
内容的提问来源于stack exchange,提问作者Victori

