vue+drf项目-前台配置

6.前台

vue环境

1
2
3
4
5
6
7
8
9
10
11
1.傻瓜式安装node: 
官网下载:https://nodejs.org/zh-cn/

2.安装cnpm:
>: npm install -g cnpm --registry=https://registry.npm.taobao.org

3.安装vue最新脚手架:
>: cnpm install -g @vue/cli

注:如果2、3步报错,清除缓存后重新走2、3步
>: npm cache clean --force

创建项目

1
2
3
4
5
"""
前提:在目标目录新建luffy文件夹
>: cd 建立的luffy文件夹
>: vue create luffycity
"""

img

img

重构项目目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
├── luffycity
├── public/ # 项目共有资源
├── favicon.ico # 站点图标
└── index.html # 主页
├── src/ # 项目主应用,开发时的代码保存
├── assets/ # 前台静态资源总目录
├── css/ # 自定义css样式
└── global.css # 自定义全局样式
├── js/ # 自定义js样式
└── settings.js # 自定义配置文件
└── img/ # 前台图片资源
├── components/ # 小组件目录
├── views/ # 页面组件目录
├── App.vue # 根组件
├── main.js # 入口脚本文件
├── router
└── index.js # 路由脚本文件
store
└── index.js # 仓库脚本文件
├── vue.config.js # 项目配置文件
└── *.* # 其他配置文件
"""

文件修订:目录中非配置文件的多余文件可以移除

App.vue

1
2
3
4
5
<template>
<div id="app">
<router-view/>
</div>
</template>

router/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter);

const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/home',
redirect: '/',
},
];

const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})

export default router

Home.vue

1
2
3
4
5
6
7
8
9
10
11
12
<template>
<div class="home">
</div>
</template>

<script>
export default {
name: 'home',
components: {
},
}
</script>

全局配置:全局样式、配置文件

global.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 声明全局样式和项目的初始化样式 */
body, h1, h2, h3, h4, h5, h6, p, table, tr, td, ul, li, a, form, input, select, option, textarea {
margin: 0;
padding: 0;
font-size: 15px;
}

a {
text-decoration: none;
color: #333;
}

ul {
list-style: none;
}

table {
border-collapse: collapse; /* 合并边框 */
}

settings.js

1
2
3
export default {
base_url: 'http://127.0.0.1:8000'
}

main.js

1
2
3
4
5
6
7
// 配置全局样式
import '@/assets/css/global.css'

// 配置全局自定义设置
import settings from '@/assets/js/settings'
Vue.prototype.$settings = settings;
// 在所有需要与后台交互的组件中:this.$settings.base_url + '再拼接具体后台路由'

7.luffy前台配置

axios前后台交互

安装:前端项目目录下的终端

1
cnpm install axios

配置:main.js

1
2
import axios from 'axios'
Vue.prototype.$axios = axios;

cookies操作

安装:前端项目目录下的终端

1
>: cnpm install vue-cookies

配置:main.js

1
2
import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;

element-ui页面组件框架

安装:前端项目目录下的终端

1
>: cnpm install element-ui

配置:main.js

1
2
3
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

bootstrap页面组件框架

安装:前端项目目录下的终端

1
2
>: cnpm install jquery
>: cnpm install bootstrap@3

配置jquery:vue.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const webpack = require("webpack");

module.exports = {
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"window.$": "jquery",
Popper: ["popper.js", "default"]
})
]
}
};

配置bootstrap:main.js

1
2
import 'bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css'

8.前端主页

图片准备

将提供的资料中的图片移植到项目的img文件夹下

页头组件:components/Header.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<template>
<div class="header">
<div class="slogan">
<p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
</div>
<div class="nav">
<ul class="left-part">
<li class="logo">
<router-link to="/">
<img src="../assets/img/head-logo.svg" alt="">
</router-link>
</li>
<li class="ele">
<span @click="goPage('/free-course')" :class="{active: url_path === '/free-course'}">免费课</span>
</li>
<li class="ele">
<span @click="goPage('/actual-course')" :class="{active: url_path === '/actual-course'}">实战课</span>
</li>
<li class="ele">
<span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">轻课</span>
</li>
</ul>

<div class="right-part">
<div>
<span>登录</span>
<span class="line">|</span>
<span>注册</span>
</div>
</div>
</div>
</div>

</template>

<script>

export default {
name: "Header",
data() {
return {
url_path: sessionStorage.url_path || '/',
}
},
methods: {
goPage(url_path) {
// 已经是当前路由就没有必要重新跳转
if (this.url_path !== url_path) {
this.$router.push(url_path);
}
sessionStorage.url_path = url_path;
},
},
created() {
sessionStorage.url_path = this.$route.path;
this.url_path = this.$route.path;
}
}
</script>

<style scoped>
.header {
background-color: white;
box-shadow: 0 0 5px 0 #aaa;
}

.header:after {
content: "";
display: block;
clear: both;
}

.slogan {
background-color: #eee;
height: 40px;
}

.slogan p {
width: 1200px;
margin: 0 auto;
color: #aaa;
font-size: 13px;
line-height: 40px;
}

.nav {
background-color: white;
user-select: none;
width: 1200px;
margin: 0 auto;

}

.nav ul {
padding: 15px 0;
float: left;
}

.nav ul:after {
clear: both;
content: '';
display: block;
}

.nav ul li {
float: left;
}

.logo {
margin-right: 20px;
}

.ele {
margin: 0 20px;
}

.ele span {
display: block;
font: 15px/36px '微软雅黑';
border-bottom: 2px solid transparent;
cursor: pointer;
}

.ele span:hover {
border-bottom-color: orange;
}

.ele span.active {
color: orange;
border-bottom-color: orange;
}

.right-part {
float: right;
}

.right-part .line {
margin: 0 10px;
}

.right-part span {
line-height: 68px;
cursor: pointer;
}
</style>

轮播图组件:components/Banner.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<template>
<div class="banner">
<el-carousel height="400px">
<el-carousel-item v-for="item in 4" :key="item">
<img src="../assets/img/banner1.png" alt="">
</el-carousel-item>
</el-carousel>
</div>
</template>

<script>
export default {
name: "Banner"
}
</script>

<style scoped>
.el-carousel__item {
height: 400px;
min-width: 1200px;
}
.el-carousel__item img {
height: 400px;
margin-left: calc(50% - 1920px / 2);
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<template>
<div class="footer">
<ul>
<li>关于我们</li>
<li>联系我们</li>
<li>商务合作</li>
<li>帮助中心</li>
<li>意见反馈</li>
<li>新手指南</li>
</ul>
<p>Copyright © luffycity.com版权所有 | 京ICP备17072161号-1</p>
</div>
</template>

<script>
export default {
name: "Footer"
}
</script>

<style scoped>
.footer {
width: 100%;
height: 128px;
background: #25292e;
color: #fff;
}

.footer ul {
margin: 0 auto 16px;
padding-top: 38px;
width: 810px;
}

.footer ul li {
float: left;
width: 112px;
margin: 0 10px;
text-align: center;
font-size: 14px;
}

.footer ul::after {
content: "";
display: block;
clear: both;
}

.footer p {
text-align: center;
font-size: 12px;
}
</style>

主页组件:views/Home.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<template>
<div class="home">
<Header />
<Banner />
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<Footer />
</div>
</template>

<script>
import Header from '../components/Header'
import Footer from '../components/Footer'
import Banner from '../components/Banner'

export default {
name: "Home",
components: {
Header,
Footer,
Banner,
}
}
</script>