#html中路由跳转 <router-link to="/"> <img src="../assets/img/head-logo.svg" alt=""> </router-link> #js中控制路由跳转 this.$router.push('/');
1.2 头部组件vue代码
# 在components内新建Head.vue <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>
# 在main.js中配置 // 配置全局样式 @ 符号,代指src路径 import '@/assets/css/global.css' // 配置全局自定义设置 import settings from '@/assets/js/settings' Vue.prototype.$settings = settings; // 在所有需要与后台交互的组件中:this.$settings.base_url + '再拼接具体后台路由' # 在assets下的css中加入global.css /* 声明全局样式和项目的初始化样式 */ 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; /* 合并边框 */ } # 在assets下的js中加入settings.js export default { base_url: 'http://127.0.0.1:8000' }
# 安装 cnpm install axios cnpm install vue-cookies cnpm install element-ui cnpm install jquery cnpm install bootstrap@3 #在main.js中配置 #axios配置 import axios from 'axios' Vue.prototype.$axios = axios; # vue-cookies配置 import cookies from 'vue-cookies' Vue.prototype.$cookies = cookies; # ElementUI的配置 import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); # bootstrap配置 import 'bootstrap' import 'bootstrap/dist/css/bootstrap.min.css' # jquery的配置,不太一样 项目根路径创一个 vue.config.js const webpack = require("webpack"); module.exports = { configureWebpack: { plugins: [ new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "window.jQuery": "jquery", "window.$": "jquery", Popper: ["popper.js", "default"] }) ] } };
1.5 页脚组件:components/Footer.vue
<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 { 100%; height: 128px; background: #25292e; color: #fff; } .footer ul { margin: 0 auto 16px; padding-top: 38px; 810px; } .footer ul li { float: left; 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>
1.6 轮播图组件:components/Banner.vue
<template> <div class="banner"> <el-carousel height="400px"> <el-carousel-item v-for="item in banner_list"> <!--<img src="../assets/img/banner1.png" alt="">--> <router-link :to="item.link"> <img :src="item.img" :alt="item.name"> </router-link> </el-carousel-item> </el-carousel> </div> </template> <script> export default { name: "Banner", data() { return { banner_list: [] } }, created() { this.$axios.get(this.$settings.base_url+'/home/banner/').then(response => { this.banner_list = response.data }).catch(error => { }) }, } </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.7 主页组件:views/Home.vue
<template> <div class="home"> <Head /> <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 Head from '../components/Head' import Footer from '../components/Footer' import Banner from '../components/Banner' export default { name: "Home", components: { Head, Footer, Banner, } } </script>
2 后端轮播图接口
2.1 轮播图表设计
utils/models.py
from django.db import models # 后期课程表,轮播图表,都会用到这些字段 class BaseModel(models.Model): create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') update_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间') is_delete = models.BooleanField(default=False, verbose_name='是否删除') is_show = models.BooleanField(default=True, verbose_name='是否展示') display_order = models.IntegerField() class Meta: abstract=True # 一定不要忘了
home/models.py
from django.db import models from luffyapi.utils.models import BaseModel # Create your models here. # 轮播图的表 class Banner(BaseModel): name = models.CharField(max_length=32, verbose_name='图片名字') img = models.ImageField(upload_to='banner', verbose_name='轮播图', help_text='图片尺寸必须是:3840*800', null=True) link = models.CharField(max_length=32, verbose_name='跳转连接') info = models.TextField(verbose_name='图片简介') # type=models.IntegerField(choices=) def __str__(self): return self.name
home/serializers.py
from rest_framework import serializers from . import models class BannerSerializer(serializers.ModelSerializer): class Meta: model = models.Banner fields = ["name", "img", "link"]
home/views.py
from rest_framework.views import APIView from luffyapi.utils.response import APIResponse from rest_framework.mixins import ListModelMixin from rest_framework.generics import GenericAPIView from rest_framework.viewsets import GenericViewSet from . import models from . import serializers from django.conf import settings # 轮播图 # class BannerView(GenericAPIView, ListModelMixin): # 路由配置:path('banner/', views.BannerView.as_view()) class BannerView(GenericViewSet, ListModelMixin): # 路由配置: path('', include(router.urls)) # 无论有多少条待展示的数据,最多就展示三条 queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('display_order')[:settings.BANNER_COUNT] serializer_class = serializers.BannerSerializer
dev.py配置文件中添加
from .const import *
luffyapi/luffyapi/settings/const.py
# 首页轮播图个数 BANNER_COUNT = 3
home/urls.py
from django.urls import path,re_path,include from rest_framework.routers import SimpleRouter from . import views router = SimpleRouter() router.register('banner', views.BannerView, 'banner') urlpatterns = [ # path('banner/', views.BannerView.as_view()) path('', include(router.urls)) ]