【发布时间】:2019-05-11 13:23:52
【问题描述】:
我想要完成的是,当页面首次加载时,我将使用 Axios 使用基本 URL 来拉回 JSON 对象。然后,我想在单击按钮时将查询参数添加到基本 URL。因此,如果单击按钮时基本 URL 是“test.com”,则会将查询参数添加到 URL 中,并且 Axios 将再次运行拉回不同的 JSON 对象。因此 URL 可能类似于“test.com?posttype=new”。
我目前正在做的是使用基本 URL 创建运行 Axios,当单击按钮时运行一个再次运行 Axios 的方法,但这一次它将查询参数添加到 URL,所以在 Vue 中它看起来像:
created () {
axios
.get(this.jobsListURL)
.then(response => (this.jobsList = response.data.data))
.catch(error => {
console.log(error)
this.errored = true
})
.finally(() => this.loading = false)
},
methods: {
getJobs: function () {
axios
.get(this.jobsListURL + '?' + this.AxiosParams)
.then(response => (this.jobsList = response.data.data))
.catch(error => {
console.log(error)
this.errored = true
})
.finally(() => this.loading = false)
},
所以在上面的示例中,jobsListURL 是 JSON 对象的基本 URL,而 AxiosParams 是我调用的一种方法,用于在单击按钮时将参数添加到 URL。因此,当单击按钮时,它正在运行 getJobs 方法以使用带有参数的 URL 重新运行 Axios。我不确定这是否是最好的方法,或者是否有更好的方法将查询参数添加到 URL 并以这种方式获取新的 JSON 对象。
只是在我走这条路线之前寻找一些反馈,看看是否有更好的方法?
【问题讨论】:
标签: javascript json vue.js axios