0👍
You need to use Template literals and they “are string literals allowing embedded expressions” so as suggested by Matt, instead of using a ” or ‘ use (backtick or `) see more here: Template literals. Moreover by using this
you can access the variable since counter
belongs to data object which is a global object and “global object is an object that always exists in the global scope” and using “this” keyword you can access a global object at global level. Read more here and here so:
created(){
axios.get(`http://jsonplaceholder.typicode.com/posts?_start=${this.counter}+0&_limit=10`).then(response => {
this.posts = response.data
})
},
- [Vuejs]-How to match an optional segment in vue-router with path-to-regexp
- [Vuejs]-How to assign a number of properties to an existing object IF that property also has nested properties inside of it?
0👍
Axios allows you to add URL query parameters as an object:
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)
})
This will yield the same result but it looks a bit more elegant and easier to maintain once you have a lot of parameters in your URLs 😊
I keep this axios cheat sheet close by for when I’m working with it.