1、使用 vue-cli 创建项目,并且安装依赖
vue-cli文档: https://cli.vuejs.org/zh/
说明:
使用 npm install XXX
命令安装如下依赖:
// package.json
"dependencies": {
"axios": "^0.19.2",
"core-js": "^3.6.5",
"element-ui": "^2.13.2",
"vue": "^2.6.11",
"vue-router": "^3.3.4",
"vuex": "^3.5.1"
},
说明:
2、开始搭建项目骨架,目录树如下:
E:\User\Desktop\qblog-client
λ tree /f
卷 个人文件 的文件夹 PATH 列表
卷序列号为 000C-C313
E:.
│ .gitignore
│ babel.config.js
│ package-lock.json
│ package.json
│ README.md
│ vue.config.js # vue-cli 配置文件
│
├─public
│ favicon.ico # 网站图标
│ index.html # 网站页面
│
└─src
│ App.vue
│ main.js
│
├─assets # 静态资源
│ logo.png
│
├─components # 自定义组件
│ NavMenu.vue
│
├─http # 发送请求相关
│ request.js
│ urls.js
│
├─router # 路由
│ index.js
│
├─store # 全局状态管理(Vuex)
│ index.js
│
└─views # 页面
Category.vue
Home.vue
Message.vue
Tag.vue
TimeLine.vue
先是在index.html
中清除内外边距
<style>
* {
margin: 0;
padding: 0;
}
style>
首先是新建vue.config.js
文件,配置代理,解决跨域问题
module.exports = {
devServer: {
proxy: {
"/api": {
target: "http://localhost:9000/api/",
pathRewrite: {
"^/api": ""
}
}
}
}
}
说明:
在@/views/
文件夹下,新建主页面Home.vue
Home
新建其他几个页面:
Category.vue:分类页面
Category {{$route.params.name}}
Message.vue:留言页面
Message
Tag.vue:标签页面
Tag {{$route.params.name}}
TimeLine.vue:时间线页面
TimeLine
新建@/router/index.js
使用vue-router组件做路由
import VueRouter from "vue-router";
import Vue from 'vue';
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: () => import("@/views/Home.vue"),
meta: {
title: "竹林客栈",
},
},
{
path: "/category/:name",
name: "Category",
component: () => import("@/views/Category.vue"),
meta: {
title: "文章分类",
},
},
{
path: "/tag/:name",
name: "Tag",
component: () => import("@/views/Tag.vue"),
meta: {
title: "标签",
},
},
{
path: "/timeline",
name: "TimeLine",
component: () => import("@/views/TimeLine.vue"),
meta: {
title: "时间线",
},
},
{
path: "/message",
name: "Message",
component: () => import("@/views/Message.vue"),
meta: {
title: "留言板",
},
},
]
const router = new VueRouter({
mode: "history",
routes
})
router.beforeEach((to, from, next) => {
console.log(from);
if (to.meta.title) {
document.title = to.meta.title;
}
next();
});
export default router;
说明:
#
的形式新建@/store/index.js
,用于设置和存储全局状态
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
token: window.sessionStorage.getItem("token"),
userInfo: null,
},
mutations: {
setToken(state, token) {
state.token = token;
window.sessionStorage.setItem("token", token);
},
removeToken(state) {
state.token = "";
state.userInfo = null;
window.sessionStorage.removeItem("token");
},
setUserInfo(state, userInfo) {
state.userInfo = userInfo;
},
},
actions: {}
});
export default store;
说明:
新建@/http/urls.js
用于存放请求地址
export default {
baseUrl: "/api/v1",
article: "/article",
articles: "/articles"
}
新建@/http/request.js
用于封装前端请求
import axios from 'axios'
import urls from "@/http/urls"
import store from "@/store/index"
import router from "@/router/index"
const instance = axios.create({
baseURL: urls.baseUrl,
timeout: 5000,
});
instance.interceptors.request.use((config) => {
if (store.state.token) {
config.headers.Authorization = store.state.token;
}
return config;
});
instance.interceptors.response.use(
(response) => {
return response;
},
(error) => {
//默认除了2XX之外的都是错误的,就会走这里
if (error.response) {
switch (error.response.status) {
case 401:
router.replace({
path: "login",
query: { redirect: router.currentRoute.fullPath }, // 将跳转的路由path作为参数,登录成功后跳转到该路由
});
}
}
return Promise.reject(error.response);
}
);
export default {
getArticles(page, limit) {
return instance.get(urls.articles, {
params: {
page: page,
limit: limit
}
}).then(res => res.data); // 这里的 res => res.data 是为了防止axios中的data属性和返回值中的data属性混淆
}
}
说明:
在main.js
中使用 Element-UI ,并且注册 store
和 router
import Vue from 'vue'
import App from './App.vue'
import store from "@/store/index"
import router from "@/router/index"
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
Vue.use(ElementUI);
new Vue({
render: h => h(App),
store,
router
}).$mount('#app')
新建@/components/NavMenu.vue
组件,封装导航栏
首页
文章分类
标签
时间线
留言板
说明:
在App.vue
中使用导航栏
<template>
<div id="app">
<nav-menu>nav-menu>
<router-view>router-view>
div>
template>
<script>
export default {
name: "App",
data() {
return {};
},
components: {
NavMenu: () => import("@/components/NavMenu.vue")
}
};
script>
<style>
.main-text {
text-decoration: none;
color: #303133;
}
.comm-text {
text-decoration: none;
color: #606266;
}
.secondary-text {
text-decoration: none;
color: #909399;
font-size: 13px;
}
.space-text {
text-decoration: none;
color: #c0c4cc;
font-size: 13px;
}
.link {
text-decoration: none;
}
.link:hover {
cursor: pointer;
color: #409eff;
}
style>
说明:
主界面:
点击导航栏可以跳转到其他页面
参考代码:https://gitee.com/qianyucc/QBlog2/tree/v-3.0