axios攔截器+mockjs

來源:互聯網
上載者:User

標籤:base   int   姓名   The   his   dex   new   日期   let   

//main.js中//引入你mock.js檔案require(‘./mock.js‘) //封裝api請求//src/axios/api.jsimport axios from ‘axios‘import vue from ‘vue‘axios.defaults.headers.post[‘Content-Type‘] = ‘application/x-www-form-urlencoded‘// 請求攔截器axios.interceptors.request.use(function(config) {return config;}, function(error) {return Promise.reject(error);})// 響應攔截器axios.interceptors.response.use(function(response) {return response;}, function(error) {return Promise.reject(error);})// 封裝axios的post請求export function fetch(url, params) {return new Promise((resolve, reject) => {axios.post(url, params).then(response => {resolve(response.data);}).catch((error) => {reject(error);})})}export default {JH_news(url, params) {return fetch(url, params);}}//src/mock.js//mock.js// 引入mockjsconst Mock = require(‘mockjs‘);// 擷取 mock.Random 對象const Random = Mock.Random;// mock一組資料const produceNewsData = function() {let articles = [];for (let i = 0; i < 100; i++) {let newArticleObject = {title: Random.csentence(5, 30), // Random.csentence( min, max )thumbnail_pic_s: Random.dataImage(‘300x250‘, ‘mock的圖片‘), // Random.dataImage( size, text ) 產生一段隨機的 Base64 圖片編碼author_name: Random.cname(), // Random.cname() 隨機產生一個常見的中文姓名date: Random.date() + ‘ ‘ + Random.time() // Random.date()指示產生的日期文字的格式,預設為yyyy-MM-dd;Random.time() 返回一個隨機的時間字串}articles.push(newArticleObject)}return {articles: articles}}// Mock.mock( url, post/get , 返回的資料);Mock.mock(‘/news/index‘, ‘post‘, produceNewsData);//使用 組件import api from ‘./../axios/api.js‘api.JH_news(‘/news/index‘, ‘type=top&key=123456‘).then(res => {console.log(res);this.newsListShow = res.articles;});

 

axios攔截器+mockjs

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.