前后端分离项目 头部,轮播于,接口

 

一:路由跳转

1.html中路由跳转

 
<router-linkto="/">
	<imgsrc="../assets/img/head-logo.svg"alt="">
</router-link>

2.js中控制路由跳转

 
this.$router.push('/');

二:Vue项目(luffycity)

1.头部Header组件

components内新建Header.vue,添加如下代码:

 
<template>
    <divclass="header">
        <divclass="slogan">
            <p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
        </div>
        <divclass="nav">
            <ulclass="left-part">
                <liclass="logo">
                    <router-linkto="/">
                        <imgsrc="../assets/img/head-logo.svg"alt="">
                    </router-link>
                </li>
                <liclass="ele">
                    <span @click="goPage('/free-course')":class="{active: url_path === '/free-course'}">免费课</span>
                </li>
                <liclass="ele">
                    <span @click="goPage('/actual-course')":class="{active: url_path === '/actual-course'}">实战课</span>
                </li>
                <liclass="ele">
                    <span @click="goPage('/light-course')":class="{active: url_path === '/light-course'}">轻课</span>
                </li>
            </ul>

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

</template>

<script>exportdefault {
        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>

<stylescoped>.header {
        background-color: white;
        box-shadow: 005px0#aaa;
    }

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

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

    .sloganp {
        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;

    }

    .navul {
        padding: 15px0;
        float: left;
    }

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

    .navulli {
        float: left;
    }

    .logo {
        margin-right: 20px;
    }

    .ele {
        margin: 020px;
    }

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

    .elespan:hover {
        border-bottom-color: orange;
    }

    .elespan.active {
        color: orange;
        border-bottom-color: orange;
    }

    .right-part {
        float: right;
    }

    .right-part.line {
        margin: 010px;
    }

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

2.配置全局css和`setting

main.js中配置

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

3.在assets下的css文件夹中加入global.css

先提前在assets中 创建cssjsimg3个文件夹

 
/* 声明全局样式和项目的初始化样式 */
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; /* 合并边框 */
}

4.在assets下的js文件夹中加入settings.js

 
export default {
    base_url: 'http://127.0.0.1:8000'
}

5.前台配置

安装

 
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中不推荐使用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"]
            })
        ]
    }
};

6.尾部组件

components内新建Footer.vue,添加如下代码:

 
<template>
    <divclass="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>exportdefault {
        name: "Footer"
    }
</script>

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

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

    .footerulli {
        float: left;
        width: 112px;
        margin: 010px;
        text-align: center;
        font-size: 14px;
    }

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

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

7.在components内新建Banner.vue,添加如下代码:

 
<template>

    <divid="banner">

        <el-carouselheight="400px">
            <el-carousel-itemv-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>exportdefault {
        name: "Banner",
        // data:function(){},data() {
            return {
                banner_list: []
            }
        },
        created() {
            //当banner组件一创建,就向后台发请求,拿回轮播图数据this.$axios.get(this.$settings.base_url + '/home/banner/').then(response => {
                console.log(response.data)
                this.banner_list = response.data
            }).catch(error => {
            })
        },

    }
</script>

<stylescoped>.el-carousel__item {
        height: 400px;
        min-width: 1200px;
    }

    .el-carousel__itemimg {
        height: 400px;
        /*margin-left: 20px;*//*margin-left: calc(50% - 1920px / 2);*/
    }
</style>

8.在routerindex.js中配置

 
const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/free-course',
        name: 'FreeCourse',
        component: FreeCourse
    }, {
        path: '/light-course',
        name: 'LightCourse',
        component: LightCourse
    }, {
        path: '/actual-course',
        name: 'ActualCourse',
        component: ActualCourse
    },

]

三:DRF项目(luffyapi)轮播图接口

1.appshomeurls.py

 
from django.urls import path, re_path, include
from . import views

from rest_framework.routers import SimpleRouter

router = SimpleRouter()
router.register('banner', views.BannerView, 'banner')
urlpatterns = [
    # path('banner/', views.BannerView.as_view()),
    path('', include(router.urls)),

]

2.appshome中的models.py

 
from django.db import models

# from luffyapi.utils.models import BaseModel
from utils.models import BaseModel


classBanner(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

3.appshome中创建serializer,并添加代码

 
from rest_framework import serializers
from . import models


classBannerModelSerilaizer(serializers.ModelSerializer):
    classMeta:
        model = models.Banner
        fields = ['name', 'link', 'img']

4.appshomeviews.py

 
from django.shortcuts import render

from rest_framework.views import APIView
from luffyapi.utils.response import APIResponse
from luffyapi.utils.logger import log

from rest_framework.mixins import ListModelMixin
from rest_framework.generics import GenericAPIView
from rest_framework.viewsets import GenericViewSet
from . import models
from . import serializer

# class BannerView(GenericAPIView,ListModelMixin):  # 这个路由配置  path('banner/', views.BannerView.as_view()),

# 路由位置
'''
from rest_framework.routers import  SimpleRouter
router=SimpleRouter()
router.register('banner',views.BannerView,'banner')
path('', include(router.urls)),
'''
from django.conf import settings


classBannerView(GenericViewSet, ListModelMixin):
    # 无论有多少条待展示的数据,最多就展示3条
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('display_order')[
               :settings.BANNER_COUNTER]
    serializer_class = serializer.BannerModelSerilaizer

5.在小luffyapi创建utils包,创建models.py

 
from django.db import models
# 后期课程表,轮播图表,都会用到这些字段

classBaseModel(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()
    classMeta:
        abstract=True  # 一定不要忘了

6.在settings文件夹中新建const.py

 
# 首页轮播图个数
BANNER_COUNTER=3

7.在dev.py中添加代码

 
from .const import *

四:添加图片

1.进入后台127.0.0.1:8000/xadmin

前后端分离项目 头部,轮播于,接口

 

2.增加Banner

前后端分离项目 头部,轮播于,接口

 

 

 

3.填好信息,保存

前后端分离项目 头部,轮播于,接口

 

 

4.进入127.0.0.1:8080,查看效果

前后端分离项目 头部,轮播于,接口

 

 

上一篇:[ Spring Boot ] 自定义启动banner


下一篇:springboot修改端口号及自定义banner