如何在咖啡脚本中将数组作为参数传递?

时间:2018-09-27 08:28:11

标签: javascript vue.js coffeescript

我正在学习coffee-script和vue.js和axio,我从https://github.com/axios/axios中找到了一个示例,如下所示

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })); 

在我的vue文件中,我写了这个

<script lang="coffee">
import axios from 'axios'

export default
    props: ['author']
    data: ->
      info: null
    mounted: ->
      vm = this
      axios
        .get 'https://api.coindesk.com/v1/bpi/currentprice.json' 
        .then (resp) -> 
          vm.info = resp
</script>

我的问题是如何翻译javascript代码

      {
        params: {
          ID: 12345
        }
      }

到咖啡脚本,传递参数目录而不声明新参数。

与发布示例相同

axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

我不知道如何在咖啡脚本中传递目录。

我尝试过这些,都失败了

.get 'https://api.coindesk.com/v1/bpi/currentprice.json' (param: {id: 1})
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' {param: {id: 1}}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' param: {id: 1}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' {id: 1}
.get 'https://api.coindesk.com/v1/bpi/currentprice.json' id: 1

谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,我发现了原因。我之间失去了一个“,”。

然后,我发现我的发布请求变成了选项请求。

然后我检查Axios call api with GET become OPTIONS

之后,我的vue文件看起来像

var average: Double = 0.0

func observeStars() {

let placesRef = Database.database().reference().child("Review")

placesRef.observe(.childAdded, with: { (snapshot) in
    let toId = snapshot.key
    var total: Double = 0.0

    placesRef.child(toId).observeSingleEvent(of: .value, with: { (snapshot) in

    let count = snapshot.childrenCount

    for childOfPlace in snapshot.children {

        let childOfPlaceSnap = childOfPlace as! DataSnapshot

        let dict = childOfPlaceSnap.value as! [String: Any]
        let val = dict["STAR"] as! Double

        total += val

        }
       self.average = total/Double(count)
       print(average)


    })

}, withCancel: nil)

}

请求看起来像

<script lang="coffee">
import axios from 'axios'
import qs from 'qs'

export default
    props: ['author']
    data: ->
      info: null
    mounted: ->
      vm = this
      axios
        .post 'http://localhost/get.request.test', qs.stringify {param: {id: 1}, name: 'phey'}
        .then (resp) -> 
          vm.info = resp
</script>