最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue multiselect options from an API - Stack Overflow

programmeradmin3浏览0评论

I am using vue multiselect and I need my options to e from an api. When I use the example from the official docs it works.

import Multiselect from 'vue-multiselect'

export default {
  ponents: {
    Multiselect
  },
  data () {
    return {
      value: [
        { name: 'Vue.js', language: 'JavaScript' }
      ],
      options: [
        { name: 'Vue.js', language: 'JavaScript' },
        { name: 'Adonis', language: 'JavaScript' },
        { name: 'Rails', language: 'Ruby' },
        { name: 'Sinatra', language: 'Ruby' },
        { name: 'Laravel', language: 'PHP' },
        { name: 'Phoenix', language: 'Elixir' }
      ]
    }
  }
}

But if I replace the options with my data it fails

data: function () {
            return {
                value: [],
                options: this.users
            }
        },
methods: {
       getUsers: function () {
            this.$http.get('/api/get/users').then(function (response) {
                    this.users = response.data;
                    this.updateUsers();
                    console.log(this.users)
                }, function (response) {
                    console.log(response)
                });
            },
            updateUsers: function () {
                this.options = this.users;
            }
        },
        created: function () {
            this.getUsers();
        }
    };

I have tried calling the method with mounted, beforeMount and beforeCreate and none of them work. My console shows the following errors:

I am sure the issue is with how am passing the options but I don't know how to pass them the right way.

For the template I am just using the default for now:

<div>
      <label class="typo__label">Simple select / dropdown</label>
      <multiselect v-model="value" :options="options" :multiple="true" :close-on-select="false" :clear-on-select="false" :hide-selected="true" placeholder="Pick some" label="name" track-by="name"></multiselect>
      <pre class="language-json"><code>{{ value  }}</code></pre>
  </div>

I am using vue multiselect and I need my options to e from an api. When I use the example from the official docs it works.

import Multiselect from 'vue-multiselect'

export default {
  ponents: {
    Multiselect
  },
  data () {
    return {
      value: [
        { name: 'Vue.js', language: 'JavaScript' }
      ],
      options: [
        { name: 'Vue.js', language: 'JavaScript' },
        { name: 'Adonis', language: 'JavaScript' },
        { name: 'Rails', language: 'Ruby' },
        { name: 'Sinatra', language: 'Ruby' },
        { name: 'Laravel', language: 'PHP' },
        { name: 'Phoenix', language: 'Elixir' }
      ]
    }
  }
}

But if I replace the options with my data it fails

data: function () {
            return {
                value: [],
                options: this.users
            }
        },
methods: {
       getUsers: function () {
            this.$http.get('/api/get/users').then(function (response) {
                    this.users = response.data;
                    this.updateUsers();
                    console.log(this.users)
                }, function (response) {
                    console.log(response)
                });
            },
            updateUsers: function () {
                this.options = this.users;
            }
        },
        created: function () {
            this.getUsers();
        }
    };

I have tried calling the method with mounted, beforeMount and beforeCreate and none of them work. My console shows the following errors:

I am sure the issue is with how am passing the options but I don't know how to pass them the right way.

For the template I am just using the default for now:

<div>
      <label class="typo__label">Simple select / dropdown</label>
      <multiselect v-model="value" :options="options" :multiple="true" :close-on-select="false" :clear-on-select="false" :hide-selected="true" placeholder="Pick some" label="name" track-by="name"></multiselect>
      <pre class="language-json"><code>{{ value  }}</code></pre>
  </div>
Share Improve this question asked Feb 22, 2017 at 6:30 Phillis PetersPhillis Peters 2,2923 gold badges21 silver badges43 bronze badges 1
  • 1 try initializing options as an empty array: [] , otherwise can you create fiddle of it? – Saurabh Commented Feb 22, 2017 at 6:32
Add a ment  | 

2 Answers 2

Reset to default 5

Try initializing options as an empty array: []

data: function () {
        return {
            value: [],
            options: []
        }
    },

Later when you get the data from backend, you can populate the data in options, as follows:

methods: {
   getUsers: function () {
        this.$http.get('/api/get/users').then( response => {
                this.options = response.data;

                console.log(this.users)
            }, function (response) {
                console.log(response)
            });
        },

In console it's saying prop 'options' expected array got undefined

mean value to this.options not assigning.

Try like this :

create variable outside this.$http request.

let self = this;

then replace :

this.options to self.options

This should work.

发布评论

评论列表(0)

  1. 暂无评论