1

(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.

Danoctum
  • 321
  • 5
  • 16

1 Answers1

1

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
Ruman
  • 191
  • 1
  • 8
  • Thanks for your quick reply, I now have changed my app.js file to match the `'vue-select'` instead of `./components/Select.vue` and I added this to my *.vue file: ` import vSelect from 'vue-select';` and `components: { vSelect }`. However, when I run my page now it gives this error `Uncaught SyntaxError: Unexpected identifier` at `vSelect from 'vue-select';` – Danoctum Jun 30 '18 at 12:01
  • Try using `require` instead of `import`. – Ruman Jun 30 '18 at 12:18
  • `require vSelect from 'vue-select';` in my *.vue file? That gives an error in the IDE, but still the same error when I refresh the page. – Danoctum Jun 30 '18 at 12:19
  • I have updated my answer, try that n let me know if you get any errors. – Ruman Jun 30 '18 at 12:23
  • Do I HAVE to install webpack/browserify for it to work? – Danoctum Jun 30 '18 at 12:23
  • What are you using currently? – Ruman Jun 30 '18 at 12:25
  • Now that I have used your changed answer, it says `(index):480 Uncaught ReferenceError: require is not defined` here: `components: { vSelect: require('vue-select') },` – Danoctum Jun 30 '18 at 12:26
  • Currently using neither of those. Just the standard Laravel 5.6 – Danoctum Jun 30 '18 at 12:26
  • check the update, i'm sure you are not aware of what laravel provides out of the box. – Ruman Jun 30 '18 at 12:33