1.vue-cli搭建框架
小白6步搞定html
2.下載vue-router
(1)npm install vue-router
(2)根目錄下建立router文件夾,並在router文件夾下建立router.js文件
(3)寫上router.js代碼vue
import Vue from 'vue' import VueRouter from 'vue-router' /** * 加載模塊 */ Vue.use(VueRouter) /** * 路由配置 */ const router = new VueRouter({ mode: 'history', likActiveClass: 'link-active', routes:[ { path: '', // 不能夠配了兩個同樣的routes , 使用重定向 redirect: { name: 'login' }, meta: { // 添加該字段, 表示登陸後才能夠訪問 // requireAuth: true, // 添加該字段, 表示該組件會被緩存 // keepAlive: true, // 添加該字段, 表示該頁面容許分享 // isShare: true } }, { path: '/login', //登陸 name: 'login', component(resolve) { require.ensure(['@/components/login/login'], () => { resolve(require('@/components/login/login')); }); } } ] }) // 路由跳轉前攔截 router.beforeEach((to,from,next) => { next() }) /** * 路由出口 */ export default router
(4)在main.js中引入router文件,並在Vue對象中註冊
(5)修改App.vue(展現路由跳轉的頁面)ios
<template> <div id="app" class="wrapper"> <transition> <router-view></router-view> </transition> </div> </template> <script> export default { name: 'App', components: { } } </script> <style> </style>
3.安裝axios(官網)vue-router
(1)npm install axios
(2)項目根目錄下建立axios文件夾,並在文件夾下建立api.js,axios.js,http.js三個js文件
api.js爲保存api文件
axios.js爲導出封裝好的對象
http.js爲二次封裝的axiosvue-cli
http.jsnpm
import axios from 'axios'; // axios 配置 axios.defaults.timeout = 5000; //http request 攔截器 axios.interceptors.request.use( config => { // const token = getCookie('名稱');注意使用的時候須要引入cookie方法,推薦js-cookie const token = getCookie('session'); //獲取Cookie config.data = JSON.stringify(config.data); config.headers = { 'Content-Type':'application/json', } if(token){ config.params = {'token':token} } return config; }, error => { return Promise.reject(err); } ); //http response 攔截器 axios.interceptors.response.use( response => { // console.log(response) if(response.data.errCode ==2){ // router.push({ // path:"/login", // querry:{redirect:router.currentRoute.fullPath}//從哪一個頁面跳轉 // }) } return response; }, error => { return Promise.reject(error) } ) /** * 封裝get方法 * @param url * @param data * @returns {Promise} */ export function get(url,params={}){ return new Promise((resolve,reject) => { axios.get(url,{ params:params }) .then(response => { resolve(response.data); }) .catch(err => { reject(err) }) }) } /** * 封裝post請求 * @param url * @param data * @returns {Promise} */ export function post(url,data = {}){ return new Promise((resolve,reject) => { axios.post(url,data, {} ) .then(response => { resolve(response.data); },err => { reject(err) }) }) } /** * 封裝patch請求 * @param url * @param data * @returns {Promise} */ export function patch(url,data = {}){ return new Promise((resolve,reject) => { axios.patch(url,data) .then(response => { resolve(response.data); },err => { reject(err) }) }) } /** * 封裝put請求 * @param url * @param data * @returns {Promise} */ export function put(url,data = {}){ return new Promise((resolve,reject) => { axios.put(url,data) .then(response => { resolve(response.data); },err => { reject(err) }) }) }
axios.jsjson
import Vue from 'vue' import axios from 'axios' import {post,get,patch,put} from '../axios/http' //定義全局變量 Vue.prototype.$post=post; Vue.prototype.$get=get; Vue.prototype.$patch=patch; Vue.prototype.$put=put;
(3)在main.js中寫引入
axios
(4)調用
api