前言:記錄項目的整個流程
按照vant官網(wǎng)的安裝-創(chuàng)建項目流程:
# 安裝 Vue Cli
npm install -g @vue/cli
# 創(chuàng)建一個項目
vue create hello-world
# Vue 2 項目鼓择,安裝 Vant 2
npm i vant -S
或
npm i vant@latest-v2 -S
2、添加依賴(持續(xù)更新)
名稱 |
用途 |
安裝命令 |
axios |
請求 |
npm install axios |
vue-router |
路由 |
npm install vue-router --save |
vuex |
狀態(tài)管理 |
npm install vuex --save |
less less |
編譯 |
npm install less less-loader --save |
lib-flexible |
適配 |
npm i lib-flexible --save |
moment |
日期處理 |
npm install moment --save |
3念搬、目錄結(jié)構(gòu)
層級 |
名稱(部分自定義) |
解釋 |
0 |
根目錄 |
項目根目錄 |
0-1 |
node-modules |
npm install后生成的依賴 |
0-2 |
public |
打包時不會被編譯朗徊,原封不動的放到dist下,相等于static,純靜態(tài)資源 |
0-3 |
src: |
主要資源文件爷恳,開發(fā)都在這個里面 |
— |
assets |
資源目錄 |
— |
api |
存放接口的地方 |
— |
components |
公共組件 |
— |
lib |
存放vant組件引用象踊,具體見后面詳解 |
— |
router |
路由 |
— |
store: |
vuex狀態(tài)管理 |
|
—modules |
細(xì)分存放狀態(tài)管理 |
|
—index |
整合所有狀態(tài)管理-總管理頁面 |
— |
utils: |
工具 |
|
—config |
存放全局常量棚壁,如服務(wù)地址铸豁,appid等 |
|
—httpServer |
存放axios配置 |
|
—tools |
常用工具 |
— |
views |
頁面存放處 |
— |
App.vue |
根組件 |
— |
main.js |
入口js文件 |
0-4 |
package.json |
依賴版本控制 |
0-5 |
readme.md |
項目介紹 |
0-6 |
vue.config.js |
vue配置,代理等 |
4节芥、代理(解決跨域問題)
devServer:{
proxy: {
"/a": {
target: "https://xxx.com.cn",
changeOrigin: true, //是否跨域
secure: false,
pathRewrite: {
'^/a': '/a' //重寫接口
},
cookieDomainRewrite: ''
}
}
}
5头镊、axios配置
//獲取cookie
const token = getCookie('access_token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
-----tools.js----
function getCookie(str) {
const name = `${str}=`;
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i += 1) {
const c = ca[i].trim();
if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
}
return null;
}
if (res.status == 200) {
return res.data
}
//存放細(xì)分后的用戶相關(guān)狀態(tài)操作等
export default new Vuex.Store({
modules: {
User坛芽,
Login
}
})
import UserApi from '@/api/User';
const userApi = new UserApi();
//存儲的公共數(shù)據(jù)
const state = {
userInfo: localStorage.getItem('userInfo') ? localStorage.getItem('userInfo') : {}
};
//處理過的數(shù)據(jù)
const getters = {
};
//唯一更改數(shù)據(jù)的方法
const mutations = {
SET_USER_INFO(state, data) {
state.userInfo = data;
}
};
//調(diào)用更改數(shù)據(jù)的方法翼抠,常用于請求后,調(diào)用mutations的方法去更改公共數(shù)據(jù)
const actions = {
async getUserInfo({ commit }) {
const res = await userApi.getUser();
if(res.result === 200){
commit('SET_USER_INFO',res.data)
localStorage.setItem('userInfo',res.data)
}else{
commit('SET_USER_INFO', {})
localStorage.removeItem('userInfo')
}
}
}
export default {
state,
getters活喊,
mutations,
actions,
};
import http from '@/utils/httpServer'
import { BASE_URL } from '@/utils/config';
export default class UserApi{
//獲取用戶信息
getUser() {
return http.get(`${BASE_URL}/user`);
}
}
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
meta:{
title:'頂部名稱'
},
component: Home
},
{
path: '/other',
name: 'other',
meta:{
title:'頂部名稱'
},
component: () => import('../views/other.vue')
}
]
const router = new VueRouter({
routes
})
router.beforeEach((to,from,next)=>{
//如果要用相同的名稱煞烫,這里做判空顯示默認(rèn)
document.title = to.meta && to.meta.title;
next()
})
export default router
8累颂、Vant使用
- npm引入后喘落,在src目錄下創(chuàng)建一個lib文件夾,里面創(chuàng)建一個vant.js
import Vue from 'vue';
import {
Icon,
Button
} from 'vant'
//按需引入
Vue.use(Icon);
Vue.use(Button);
- 在main.js中引入vant.js,代碼中即可使用vant組件
import './lib/vant';