javascript - 如何在链接中写属性?
问题描述
我是 Vue.js 的新手,并且执行一项任务,我需要将属性写入链接,但我不知道该怎么做?如何将“计数器”从“数据”写入链接以使其正常工作。
export default {
name: 'app',
data () {
return {
counter: 1,
}
},
created(){
axios.get('http://jsonplaceholder.typicode.com/posts?
_start=${counter}+0&_limit=10').then(response => {
this.posts = response.data
})
}
}
解决方案
Axios 允许您将 URL 查询参数添加为对象:
axios.get('http://jsonplaceholder.typicode.com/posts', {
params: {
_start: this.counter, //or `${this.counter}+0` if you need that +0 as a string at the end
_limit: 10
}
})
.then(function (response) {
this.posts = response.data
})
.catch(function (error) {
console.log(error)
})
这将产生相同的结果,但是一旦您的 URL 中有很多参数,它看起来会更优雅且更易于维护
当我使用它时,我将这个 axios 备忘单放在附近。
推荐阅读
- amazon-web-services - 单一管理员的 Cognito 用户池
- ruby-on-rails - 如何查询具有 ActiveStorage 附件的记录?
- nlp - 如何在聊天机器人中实现本体?
- mysql - 我想在 docker 容器中创建一个 MySql 数据库,并在容器启动时在其中执行一些 DDL 和 DML 语句
- php - 错误 org.json.JSONException:在字符 0 处输入结束
- data-structures - 如何在队列和堆栈的帮助下对双向链表进行排序?
- angularjs - 嵌套的 ng-repeat 未显示正确的值
- java - Dagger 2 无法进行构造函数注入
- c# - 如何使用 ajax 将动态表加载到 Razor 页面
- javascript - 如何在 Angular 中使用来自外部 js 的方法