Laravel 7 and VueJs Vue Multiselect Noob Question

百般思念 提交于 2021-01-29 07:01:09

问题


I am a totally noob at laravel and npm and vuejs things. I made a new Laravel Project and instead of playing around with jquery I want to learn how to use vuejs.

I ran against a wall today :( trying 2 days to get this Multiselect (https://vue-multiselect.js.org/#sub-select-with-search) running on my project. I think I am missing some basics ... What I've done: ran on terminal npm install vue-multiselect created in resources/js/comonents/Multiselect.vue pasted this code in /Multiselect.vue:

<template>
    <div>
        <multiselect
            v-model="selected"
            :options="options">
        </multiselect>
    </div>
</template>

<script>
    import Multiselect from 'vue-multiselect'
    export default {
        components: { Multiselect },
        data () {
            return {
                selected: null,
                options: ['list', 'of', 'options']
            }
        }
    }
</script>

<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

added to my app.js in resources folder:

 - import Multiselect from "vue-multiselect";
   - Vue.component('v-multiselect', require('./components/Multiselect.vue'));
   - const app = new Vue({
   -    el: "#app",
   -    data: {
   -  users: '',
   -  firmas: '',

}});

and in my blade file I used:

  <v-multiselect></v-multiselect>

So far ... so good npm run dev and refreshed the page.

Error: 
index.js:133 [Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <VMultiselect>
       <Root>

so I have two questions is this the correct way to implement external vuejs components inte Laravel ? and what If it is the right way am I doing wrong - at which points???

Thank you all out there to help me to learn ...


回答1:


I'm glad you got your code working! To answer your question, it looks like you're using a mix of the external component you're importing and your own custom component which uses that component which may be what is confusing you a little bit.

When you do the following:

import Multiselect from "vue-multiselect";

inside your app.js file, you are importing an external component globally. When you place that import inside of a component, you are importing the external component for use within that component only. In your current code you've posted, you are importing it both globally and within your component.

If you are registering a component globally (within the element id assigned to the vue instance), you can register it like this within your app.js file:

import Multiselect from "vue-multiselect";
Vue.component('multiselect', Multiselect);

Then in your components, you will not have to import it again, but simply use it like this:

<template>
    <div>
        <multiselect v-model="selected" :options="options" placeholder="Select one" label="name" track-by="name"></multiselect>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                selected: null,
                options: ['one','two','three'],
            }
        },
    }
</script>

You would also be able to use this component in your blade since it is defined within your app.js file.

However with the setup you're using now, your fix of:

Vue.component('v-multiselect', require('./components/Multiselect.vue').default);

is not actually registering the external component. You are registering YOUR component.

So to answer your question, yes, you've taken an external component where you can make your custom component and easily add reusable content around it which is perfectly valid use, but you could either remove the extra import of Multiselect in your app.js file, or import the Multiselect external component globally, like I mentioned above.




回答2:


Update: Found the solution for my problem: in my app js there was the error!

Vue.component('v-multiselect', require('./components/Multiselect.vue').default);

I registered the component wrong :( So second question is answered :) But do you guys do it the same way? or I am completly wrong implementing external commponets into laravel? I don't want to use vueex or vuerouter for now ... I need to learn laravel itself ... afterwards I know how Laravel works I will use vuerouter ... for my projects ... So sorry for the long text - but needed to explain a little bit more about the situation - thnaks for reading guys!




回答3:


Thank you very much Sawyer,

I got it, I thought :( I want to use this multiselect component muptliple times in my page. So I removed the extra import in my app.js - saw it in phpstorm that it was unused but didn't know why :) - now I know.

What I have so far:

  • hit me if I am wrong :) in app.js: (registering my own component)

    Vue.component('v-multiselect', require('./components/Multiselect.vue').default);
    

added Multiselect.vue to my components folder in laravel with this src:

<template>
    <div>
        <multiselect v-model="value" :options="options"></multiselect>
    </div>
</template>

<script>
    import Multiselect from 'vue-multiselect'

    // register globally
    Vue.component('multiselect', Multiselect)

    export default {
        // OR register locally
        components: { Multiselect },
        data () {
            return {
                value: null,
                options: ['option1','option2','option3']
            }
        }
    }
</script>

<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

and in my blade file:

 <v-multiselect :options="['one','two','three']" ></v-multiselect>

I get no errors at all from vuejs butit isn't working as it should: How do I overwrite the options array from my blade file ? As I saw on the documentation "options" is a prop of the component so why am I getting as select the predefined option array ['option1','option2','option3'] and not the array from the blade file:['one','two','three'] am I missing a shortcut or something else?? Thanks a lot for your patience ... If you can tell me where to read about it - except the docs of vuejs - I think this will help me a lot!



来源:https://stackoverflow.com/questions/63393255/laravel-7-and-vuejs-vue-multiselect-noob-question

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!