詳解在vue-cli項(xiàng)目中使用mockjs(請求數(shù)據(jù)刪除數(shù)據(jù))

在我們的生產(chǎn)實(shí)際中,后端的接口往往是較晚才會(huì)出來,于是我們的前端的許多開發(fā)都要等到接口給我們才能進(jìn)行,這樣對于我們前端來說顯得十分的被動(dòng),于是有沒有可以制造假數(shù)據(jù)來模擬后端接口呢,答案是肯定的。于是今天我們來介紹一款非常強(qiáng)大的插件Mock.js ,可以非常方便的模擬后端的數(shù)據(jù),也可以輕松的實(shí)現(xiàn)增刪改查這些操作,在后臺(tái)數(shù)據(jù)完成之后,你所做的只是去掉mockjs:停止攔截真實(shí)的ajax,僅此而已。

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站制作設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)公司擁有實(shí)力堅(jiān)強(qiáng)的技術(shù)研發(fā)團(tuán)隊(duì)及素養(yǎng)的視覺設(shè)計(jì)專才。

搭建一個(gè)vue項(xiàng)目

# 全局安裝 vue-cli
$ npm install --global vue-cli
# 創(chuàng)建一個(gè)基于 webpack 模板的新項(xiàng)目
vue init webpack vue-mock
$ cd my-project
# 安裝依賴
$ npm install

安裝mockjs

npm install mockjs --save-dev

開啟項(xiàng)目

npm run dev

創(chuàng)建一個(gè)mockjs文件夾以及mockjs,并且在main.js引入這個(gè)文件

此時(shí)可以看到像這樣的一個(gè)項(xiàng)目結(jié)構(gòu)

詳解在vue-cli項(xiàng)目中使用mockjs(請求數(shù)據(jù)刪除數(shù)據(jù))

mockjs的使用

在項(xiàng)目中的mock.js文件中,寫入模擬的數(shù)據(jù),此時(shí)我們可以參照一下mockjs的文檔。

// 使用 Mock
var Mock = require('mockjs')
var data = Mock.mock({
  // 屬性 list 的值是一個(gè)數(shù)組,其中含有 1 到 10 個(gè)元素
  'list|1-10': [{
    // 屬性 id 是一個(gè)自增數(shù),起始值為 1,每次增 1
    'id|+1': 1
  }]
})
// 輸出結(jié)果
console.log(JSON.stringify(data, null, 4))

接下來可以做我們想要做的事了

在mock.js中模擬簡單的一些數(shù)據(jù)

 const Mock = require('mockjs');
// 獲取 mock.Random 對象
 const Random = Mock.Random;
 // mock一組數(shù)據(jù)
 const produceData = function (opt) {
  console.log('opt', opt);
  let articles = [];
  for (let i = 0; i < 30; i++) {
   let newArticleObject = {
    title: Random.csentence(5, 30), // Random.csentence( min, max )
    thumbnail_pic_s: Random.dataImage('300x250', 'mock的圖片'), // Random.dataImage( size, text ) 生成一段隨機(jī)的 Base64 圖片編碼
    author_name: Random.cname(), // Random.cname() 隨機(jī)生成一個(gè)常見的中文姓名
    date: Random.date() + ' ' + Random.time() // Random.date()指示生成的日期字符串的格式,默認(rèn)為yyyy-MM-dd;Random.time() 返回一個(gè)隨機(jī)的時(shí)間字符串
   }
   articles.push(newArticleObject)
  }
  return {
   data: articles
  }
 }
Mock.mock('/news', /post|get/i, produceData);//當(dāng)post或get請求到/news路由時(shí)Mock會(huì)攔截請求并返回上面的數(shù)據(jù)

在vue中請求

methods: {
   setNewsApi: function() {
    this.$http.post("/news", "type=top&key=123456").then(res => {
     console.log(res.data);
 
     this.newsListShow = res.data.data;
    });
   }
  }

效果預(yù)覽

詳解在vue-cli項(xiàng)目中使用mockjs(請求數(shù)據(jù)刪除數(shù)據(jù))

再做一個(gè)刪除的處理

模擬數(shù)據(jù)

let arr = []
 for (let i = 0; i < 30; i++) {
  let newArticleObject = {
   name: Random.cname(), // Random.cname() 隨機(jī)生成一個(gè)常見的中文姓名
   content: Random.csentence(5, 30), // Random.csentence( min, max )
   id: i
  }
  arr.push(newArticleObject);
 }
 let list = function (options) {
  let rtype = options.type.toLowerCase(); //獲取請求類型
  switch (rtype) {
   case 'get':
    break;
   case 'post':
    let id = parseInt(JSON.parse(options.body).params.id) //獲取刪除的id
    arr = arr.filter(function(val){
     return val.id!=id;//把這個(gè)id對應(yīng)的對象從數(shù)組里刪除
    });
    break;
   default:
  }
  return {
   data: arr
  } //返回這個(gè)數(shù)組,也就是返回處理后的假數(shù)據(jù)
 }
 Mock.mock('/list', /get|post/i, list);//get用于請求數(shù)據(jù),post用于刪除數(shù)據(jù)

vue中使用

methods: {
   setNewsApi: function() {
    this.$http.get("/list", "").then(res => {
     this.data = res.data.data;
    });
   },
   deleteList(data) { //刪除數(shù)據(jù)
    let id = data.id;
    this.$http.post('/list', {
      params: {
       id: id
      }
     }).then(function(res) {
      console.log(res);
      this.data = res.data.data;
      alert(data.name + '刪除成功');
     }.bind(this))
     .catch(function(error) {
      console.log(error)
     });
   },
  }

效果預(yù)覽

 詳解在vue-cli項(xiàng)目中使用mockjs(請求數(shù)據(jù)刪除數(shù)據(jù))

github代碼地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前題目:詳解在vue-cli項(xiàng)目中使用mockjs(請求數(shù)據(jù)刪除數(shù)據(jù))
轉(zhuǎn)載源于:http://muchs.cn/article36/jpdhpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)服務(wù)器托管、做網(wǎng)站外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作