import js file from public folder in a vue component (Laravel) - laravel

I need to import a file "js" from folder "public" in a vue component.
my Vue component:
<template>
<div>
<h2>Hello</h2>
</div>
</template>
<script>
import {myplugin} from "/public/vendor/jquery/jquery.min.js";
export default {
name: "pageone"
}
</script>
but i receive an error in compiling phase, can someone help me?

(Although it's not a very good idea to place your libraries/packages to public folder when using compilers), if you really want to place it in public folder, you should use a relative_path.
import {myplugin} from "../../public/vendor/jquery/jquery.min.js";
However, I'd install it as npm package and leverage laravel-mix for adding jquery in my build.

No need to import it.
Just add a <script src="/path/to/your.js"> into head section of public/index.html.

Related

Error importing script in vue file in Vue 3

I'm using laravel-mix with Vue 3 and I'm getting the following error:
ERROR in ./resources/js/components/App.vue 2:0-54
Module not found: Error: Can't resolve './App.js/?vue&type=script&lang=js' in './resources/js/components'
components/App.vue
<template>
<div id="app">
<h1>Hello world</h1>
</div>
</template>
<script src="./App.js/"></script>
components/App.js
export default {
name: "App",
};
In my previous projects with Vue 2 and Laravel 7 I didn't get these errors. Now I'm using Vue 3 and Laravel 8, but don't know how to fix it. I have installed vue-loader and vue-template-compiler so I guess it should work.
Does Vue 3 allow importing a script from a separated js file? In Vue 2 that was possible (I like to separate the HTML from the JS because of some issues with my editor and also for better organization.)
This not related to Vue3, just you added slash after file so it's understanding as a dir.
<script src="./App.js/"></script>
to
<script src="./App.js"></script>

Laravel + Flatpicker: How to Install using NPM

Did is what I did so far
npm i flatpickr --save
Add #import "~flatpickr/dist/flatpickr.css"; to the app.scss
index.blade.php
#section('content')
<div class="form-group">
<label for="loaned">Loaned</label>
<input type="date" class="form-control" name="loaned" id="loaned">
</div>
#endsection
#section('scripts')
<script>
flatpickr('#loaned')
</script>
#endsection
I get an error, "flatpickr is not defined". I read the documentation but it, I don't know what to do with this part:
// commonjs
const flatpickr = require("flatpickr");
// es modules are recommended, if available, especially for typescript
import flatpickr from "flatpickr";
You can add do this in one of two ways:
In your app.js (usually under the resources folder):
import flatpckr from 'flatpickr';
window.flatpckr = flatpckr;
This will expose the flatpckr function in your global window object.
An alternative is to add all your script code in your script files instead of in the blade files e.g.:
import flatpckr from 'flatpickr';
// Maybe add conditions on when to run this
flatpckr('#loaned');
This way you don't "pollute" your window object with a bunch of libraries without needing to.
In this case I add the following line to webpack.mix.js
mix.copy('node_modules/flatpickr/dist/flatpickr.css', 'public/css/flatpickr.css');
and import the css file the regular way.
<link rel="stylesheet" href="/css/flatpickr.css" />
Addition: of course you need to run npm run dev and if you use versioning, add the file to your git.
This works for me:
npm i flatpickr --save
Register flatpickr in the resources/js/app.js
require('./bootstrap');
require('alpinejs');
require("flatpickr");
Add an import flatpickr in the resources/css/app.css
#import 'flatpickr/dist/flatpickr.css';
Run the npm run dev or npm run watch.

Import npm package in a .blade

I use laravel and vue js in a project that I am developing on. I have installed vue-color npm package in-order to load a colour picker.
npm package installed successfully. But when I try to import it to the blade it shows the error below.
Uncaught SyntaxError: Cannot use import statement outside a module
The below code will show how I used inside the blade.
#section
<div>
.......
</div>
#endsection
#push('script')
<script>
import Photoshop from 'vue-color';
var manageDisplayStore = new Vue({
el: '#containerMain',
name: 'manageDisplayStore',
components: {
// Photoshop
},
How can I import the package?
You can import NPM packages in your /resources/js/app.js, then include /resources/js/app.js in your layout template and only then call it within a Blade template.
The app.js most probably has inclusions of NPM packages (like Bootstrap / Vue) if you've used one of the default presets when installed Laravel.
Assign the import to global variable something like window.Photoshop = require('vue-color');
The app.js should be included in your layout by default as well.
Then use it in a Blade template like you've used. Photoshop or window.Photoshop variable should be available.
Check for the document being ready before usage or it could be undefined depending where you import the app.js.

How do you integrate a Vue.js package (like vodal) into Laravel?

I have an empty Laravel project and have been trying to install vodal
(https://github.com/chenjiahan/vodal) into it, with no luck.
I know the basics of vue.js but am still a beginner and have never used a Vue.js package in my Laravel app before.
Note: I was able to download https://github.com/chenjiahan/vodal and get it running as a standalone. The challenge is getting it integrated into a new Laravel 5.8 project.
After running:
npm i -S vodal
Where does this code go in my laravel app? What should go in:
app.js
a blade.php view file
a new Vue component
any other location?
How do I get this Vodal (or for that matter, any vue.js) package to work with Laravel? I've been struggling for hours on end, and ANY help would be appreciated.
<vodal :show="show" animation="rotate" #hide="show = false">
<div>A vue modal with animations.</div>
</vodal>
import Vue from 'vue';
import Vodal from 'vodal';
Vue.component(Vodal.name, Vodal);
export default {
name: 'app',
data() {
return {
show: false
}
}
}
// include animation styles
#import "vodal/common.css";
#import "vodal/rotate.css";
In laravel first open terminal and install npm with the command npm install after that you can see a new folder in your project named as node_modules. all your packages code is inside this folder.
Now simply run your command for vodal like npm i -S vodal
now you can simply import this package into your app.js file as you did.
Run npm run watch for development mode which will import all vodal code from node_modules folder into your app.js file which is inside your public directory.
Update
I saw your repository and I downloaded it and edit some of your files. you were doing totally wrong.
I suggest you to learn vuejs first rather than getting deep into it.
the problem was you calling it in vue, not in the vue component so why you give the following code.
export default {
components: {
Vodal
},
data() {
return {
show: false
}
}
}
we do this in vue component, not in vue.
in data, we return only in vue component and the official documentation of vodal, they also give an example of using it in vue component.
and you not using it in vue component so simply did this in vue like below
const app = new Vue({
el: '#app',
data:{
show: false
}
});
So Now Your CSS.
so for including css. i simply import this in app.scss file like below.
#import "~vodal/common.css";
#import "~vodal/rotate.css";
now the final part and your biggest mistake is.
why you don't include app.js file in welcome.blade file. All your code is written in app.js file and you are not including it. so i simply include it in welcome.blade file like below
<script src="{{ asset('js/app.js') }}" defer></script>
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
after all, just run npm run watch
so now the main part you need to show:true for show modal. by default is false so it will not show you.
am making a click event for the show , so you understand how it's all going to work. and ill share files with the link in the comment.

Laravel how do I import a vuejs package installed trough NPM

(Disclaimer: I'm not very proficient in Vue)
What is the correct way to import/include a Vuejs package in my project? I somehow seem to not be able to import a file.
I'm trying to import this package: https://github.com/sagalbot/vue-select
What I have done so far:
Ran the command: npm install vue-select
Tried adding the vue-select package to my Vue like so (in
resources/assets/js/app.js):
import vSelect from './components/Select.vue'
Vue.component('v-select', vSelect);
Now when I use the v-select in my HTML file it gives this error in console: [Vue warn]: Unknown custom element: <v-select> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
What I've tried to fix this:
Instead of using this in app.js I used it in my *.vue file, I don't use webpack/browserify I think, so import doesnt work :/ I prefer not to use browserify.
I have vue-resource package aswell and that created a vue-resource.js file in my public/js directory which I can import to use vue-resource. It seems like vue-select doesn't create its own vue-select.js file.
If you have any suggestions, please let me know.
I have looked at this question: Import vue package in laravel but I don't understand what paths and files the lines should be added to.
I think this is hust your import statement. If you have installed it through npm, then you should import from vue-select. Also, use require instead of import, This way:
Vue.component('v-select', require('vue-select'));
You may want to use it only in your Vue component, then you should load your own component as explained above, And in your own vue component say VueSelect.vueuse this:
<template>
<!-- use it with name 'v-select' in your component -->
<v-select></v-select>
</template>
<script>
export default {
...
components: { vSelect: require('vue-select') },
...
}
</script>
Then you can register your own component a s global,
Vue.component('vue-select', require( './components/VueSelect.vue') );
And use it in your root component:
<vue-select></vue-select>
Update
If you are using laravel, it gives you laravel mix out of the box which compiles down your assets using webpack. All you need to do is run a compile script:
npm run dev
If you are making changes to your js files, you wouldn't want to run this over and over, so you can run watch command:
npm run watch
If this doesnt work for you, try watch-poll instead. Als, make sure you have installed all dependencies first:
npm install

Resources