What error do you get?
How to use a vue-related npm package in laravel app
Hi, everyone! As many people here, I'm fairly new to Vue. I've watched all of the Vue 2.0 videos here at Laracasts and done all my homework (with a bit of success), jet I'm struggling to find a solution to what looks to be a very simple problem: how do I use in a Laravel app a vue-related npm package?
As an example, I've been trying to use Vue.Draggable, and following their instructions, I've done the following:
Installed the package:
npm install vuedraggable --save
Then, in my app.js file I've required vuedraggable:
var draggable = require('vuedraggable');
Finally, in my blade view I've inserted their sample code:
<div id="app">
<draggable :list="list" @start="drag=true" @end="drag=false">
<div v-for="element in list">{{element.name}}</div>
</draggable>
</div>
<script>
Vue.component('draggable', {
import draggable from 'vuedraggable',
export default {
components: {
draggable,
}
}
});
new Vue({
el: "#app",
data: {
list: [
{ name: "Some element", id: 1 },
{ name: "Some other element", id: 2 },
{ name: "An extra element", id: 3 },
{ name: "Final element", id: 4 }
}
});
</script>
And this clearly doesn't work. Any help would be hugely appreciated! Thanks!
Hey,
I recently found this by googling the exact same problem. I fixed it the following way:
Install npm package and reference as component:
Install vuedraggable and add it to package.json:npm install vuedraggable --save
Then, in your app.js or bootstrap.js or whereever you require your vue, do the same for vuedraggable:
window.draggable = require('vuedraggable');
Now you can reference the "draggable" variable as a component within your Vue instance, like so:
new Vue({
el: '#app',
components: {
draggable,
},
data: {
fields: [{name: "test1"}, {name: "test2"}],
},
});
which would work with the following HTML markup:
<div id="app">
<draggable :list="fields" class="dragArea">
<div v-for="element in fields">@{{ element.name }}</div>
</draggable>
</div>
Hope this helps somebody :-)
Please or to participate in this conversation.