如何在链接中写入属性?

时间:2019-03-13 18:04:50

标签: javascript vue.js axios

我是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
    })
  }
}

2 个答案:

答案 0 :(得分:0)

您需要使用模板文字,并且它们“是允许嵌入表达式的字符串文字”,正如Matt所建议的那样,而不是使用“或'use(反引号或`),请参见此处:{ {3}}。此外,由于this属于作为全局对象的数据对象,而“ 全局对象是始终存在的对象”,因此您可以访问变量在全局范围内”,并使用“ ”关键字可以访问全局级别的全局对象。了解更多Template literalshere的信息,

counter

答案 1 :(得分:0)

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中包含许多参数,它看起来就会更加优雅并且易于维护

在使用this axios cheat sheet时,我一直在附近。