res.data.amount一直返回不了价格是怎么回事?

来源:7-7 购买详情页 - 总价计算(2)

野火燎原

2017-04-30

  代码是直接复制过来的,但是页面载入时,db.json 的字段会直接变成下面这样,就算一开始在db.json加入了价格,页面重载时还会变成下面这样,不能获取价格

db.json

"getPrice": {"amount": 123}

页面载入时变为

"getPrice": {

    "buyNum": 1,

    "buyType": 0,

    "period": 0,

    "version": "0"

  },

没有获取"getPrice": {"amount": 123},而是修改了"getPrice"的内容。

analysis.vue

onParamChange (attr, val) {

      this[attr] = val

      this.getPrice()

    },

getPrice () {

      let buyVersionsArray = _.map(this.versions, (item) => {

        return item.value

      })

      let reqParams = {

        buyNum: this.buyNum,

        buyType: this.buyType.value,

        period: this.period.value,

        version: buyVersionsArray.join(',')

      }

      this.$http.post('/api/getPrice', reqParams)

      .then((res) => {

        this.price = res.data.amount

      })

    }

mounted () {

    this.buyNum = 1

    this.buyType = this.buyTypes[0]

    this.versions = [this.versionList[0]]

    this.period = this.periodList[0]

    this.getPrice()  

  }


写回答

2回答

fishenal

2017-05-02

页面重载后还会变成哪样?db.json里是假数据,页面里改动的只是浏览器里的js数据,改不了db.json里的数据

0
6
fishenal
json-server也不是不正常,它只是把post理解为修改,get才是请求数据,而实际项目中通常用post来获取数据
2017-05-03
共6条回复

fishenal

2017-05-01

你的发送请求代码是什么样的?是不是用了 json-server? 用json-server的话 post请求会改变db.json里的值

0
1
野火燎原
代码都是源码,不用json-server的话 this.$http.post('/api/getPrice', reqParams)里的api不是就不能生效了?视频看下来里面好像也没说要禁用掉json-server。如何解决
2017-05-02
共1条回复

最容易上手的Vue2.0入门实战教程

快速入门Vue2.0,组件化开发一个数字产品电商平台

3966 学习 · 999 问题

查看课程