Vue.js Router Query Array

戏子无情 提交于 2019-12-02 12:19:52

问题


I'm trying to do is pass an array from query to the backend using Vue.js and router.

So I have this method:

submitForm () {
  this.$router.push({
    name: 'AuctionResult',
    query: {
      models: this.selectedModels.map(e => e.value)
    }
  })
},

As a result will be query like this: ?models=MODEL1&models=MODEL2... But how can I make inputs look like array, like this: ?models[]=MODEL1&models[]=MODEL2... ???

I didn`t find anything in the documentation.


回答1:


To support PHP / array style multi-values, you can just set the key name to be what you want, ie

query: {
  'models[]': this.selectedModels.map(e => e.value)
}

This may come out as

?model%5B%5D=MODEL1&model%5B%5D=MODEL2...

but that's fine (it's just URL encoded) and your server-side request handler should decode it correctly.



来源:https://stackoverflow.com/questions/50692081/vue-js-router-query-array

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