mock數據的應用

mock數據

vue本地開發 自己寫mock數據

  • 引入假數據
  • 配置假數據
  • 訪問假數據

引入假數據

在目錄下的static(數據多的情況下) 或者 根目錄(數據少的情況下)下引入json數據

配置假數據

在bulid目錄下的dev-server.js文件中配置

“`
const app = express()
//my ON
let appData = require(‘../data’)

let seller = appData.seller
let goods = appData.goods
let ratings = appData.ratings
let apiRouters = express.Router()

apiRouters.get(‘/seller’,function(reg,res){
res.json({
errno: 0,// 數據正常返回0
data: seller
})
})

apiRouters.get(‘/goods’,function(reg,res){
res.json({
errno: 0,
data: goods
})
})

apiRouters.get(‘/ratings’,function(reg,res){
res.json({
errno: 0,
data: ratings
})
})

app.use(‘/api’,apiRouters)

// my END“`

訪問假數據

安裝vue-resource (作用 同ajax)
localhost:8080/aip/goods

created () {
this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
this.$http.get('./api/goods').then((result) => {
result = result.body
if (result.errno === ERROK) {
this.goods = result.data
/* console.log(this.goods) */
// 數據加載完畢讓 better-scroll 起作用 因爲異步要放在$nextTick()中
this.$nextTick(() => {
this._initScroll()
this.__calculateHeight()
})
}
})
}


瀏覽器兼容

1.vue應用了許多es6的語法因此 不兼容IE8及以下瀏覽器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章