luffy 前臺搭建和站點配置

1、環境

1.傻瓜式安裝node: https://nodejs.org/zh-cn/
2.安裝cnpm: npm install -g cnpm --registry=https://registry.npm.taobao.org
3.安裝vue/cli 3.x: cnpm install -g @vue/cli
4.若是報錯處理後從新走二、3步:npm cache clean --force

 

2、建立項目

前提:在目標目錄新建luffy文件夾
cd 創建的luffy文件夾
vue create luffycity

 

3、重構項目目錄

├── luffycity
    ├── public/                       # 項目共有資源
    │    ├── favicon.ico               # 站點圖標
    │    ├── index.html                # 主頁
    ├── src/                           # 項目主應用,開發時的代碼保存
    │    ├── assets/                   # 前臺靜態資源總目錄
    │    │    ├── css/                 # 自定義css樣式
    │    │    │    ├── global.css      # 自定義全局樣式
    │    │    ├── js/                  # 自定義js樣式
    │    │    ├── img/                 # 前臺圖片資源
    │    ├── components/               # 小組件目錄
    │    ├── views/                    # 頁面組件目錄
    │    ├── App.vue                   # 根路由
    │    ├── main.js                   # 入口腳本文件
    │    ├── router.js                 # 路由腳本文件
    │    ├── settings.js               # 自定義配置文件
    │    ├── store.js                  # 倉庫基本文件
    └── vue.config.js                  # 全局配置文件

 

4、配置開發環境

一、配置全局樣式

①在assets/css新建global.css,用於完成全局樣式設置,如css 的 reset 操做
color {
    margin: 0
}
②在main.js文件中加載全局樣式文件
import "@/assets/css/global.css";

二、配置全局配置文件

①在src目錄下新建settings.js文件,在這個文件中能夠完成配置請求後臺的根路由頭,之後就能夠統一請求接口的前綴css

export default {
    Host: 'http://localhost:8000/',
}

②在main.js文件中加載全局配置文件,添加到Vue原型,在Vue項目的邏輯中 this.$settings.Host 就能夠拿到Host的內容 'http://localhost:8000/'html

import settings from "@/settings"
Vue.prototype.$settings = settings;

③在前端項目目錄下安裝element-ui前端

cnpm install element-ui --save

④在main.js中配置element-uivue

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

 

5、配置站點

一、修改hosts文件

hosts文件涉及的dns解析node

1、找到hosts文件
windows: C:\Windows\System32\drivers\etc\hosts
mac|linux:/etc/hosts

2、在host文件中新增下面兩句話,文件修改權限本身解決
127.0.0.1    www.luffy.cn
127.0.0.1    api.luffy.cn

 

二、前臺站點配置

在 vue.config.js 項目配置文件中
module.exports = {
    devServer: {
        host: 'www.luffy.cn',
        port: 8080
    }
}

 

三、後臺配置

settings/dev.py配置

# 後臺連接的白名單
ALLOWED_HOSTS = [
    'api.luffy.cn'
]

 

6、前端主頁

一、準備

按照下圖將該刪除的刪除,在增長的增長,圖片資源在課件的資源中python

二、各文件修改內容

①vue.config.js 項目配置文件
module.exports = {
    devServer: {
        host: 'www.luffy.cn',
        port: 8080
    }
}

main.js 根腳本

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false;

import '@/assets/css/global.css'
import settings from '@/settings'
Vue.prototype.$settings = settings;

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

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app');

③settings.js 前臺交互配置

export default {Host: 'http://localhost:8000/',}

router.js 路由腳本

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'

Vue.use(Router);

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home
        }
    ]
})

App.vue 根組件

<template>
    <div id="app">
        <router-view/>
    </div>
</template>

<script>
    export default {
        name: 'App'
    }
</script>

⑥Home.vue 頁面組件

<template>
  <div class="home">
    <Header></Header>
    <Banner></Banner>
    <Footer></Footer>
  </div>
</template>

<script>
    import Header from "@/components/Header"
    import Banner from "@/components/Banner"
    import Footer from "@/components/Footer"

    export default {
        name: "Home",
        data() {
            return {}
        },
        methods: {},
        components: {
            'Header': Header,
            Footer,
            Banner,
        }
    }
</script>

<style scoped>

</style>

Header.vue 小組件

<template>
    <div class="header-box">
        <div class="header">
            <div class="content">
                <div class="logo full-left">
                    <router-link to="/"><img @click="jump('/')" src="@/assets/img/logo.svg" alt=""></router-link>
                </div>
                <ul class="nav full-left">
                    <li><span @click="jump('/course')" :class="this_nav=='/course'?'this':''">免費課</span></li>
                    <li><span @click="jump('/light-course')" :class="this_nav=='/light-course'?'this':''">輕課</span></li>
                    <li><span>學位課</span></li>
                    <li><span>題庫</span></li>
                    <li><span>老男孩教育</span></li>
                </ul>
                <div class="login-bar full-right">
                    <div class="shop-cart full-left">
                        <img src="@/assets/img/cart.svg" alt="">
                        <span><router-link to="/cart">購物車</router-link></span>
                    </div>
                    <div class="login-box full-left">
                        <span>登陸</span>
                        &nbsp;|&nbsp;
                        <span>註冊</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Header",
        data() {
            return {
                this_nav: "",
            }
        },
        created() {
            this.this_nav = localStorage.this_nav;
        },
        methods: {
            jump(location) {
                localStorage.this_nav = location;
                // vue-router除了提供router-link標籤跳轉頁面之外,還提供了 js跳轉的方式
                this.$router.push(location);
            }
        }
    }
</script>

<style scoped>
    .header-box {
        height: 80px;
    }

    .header {
        width: 100%;
        height: 80px;
        box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        margin: auto;
        z-index: 99;
        background: #fff;
    }

    .header .content {
        max-width: 1200px;
        width: 100%;
        margin: 0 auto;
    }

    .header .content .logo {
        height: 80px;
        line-height: 80px;
        margin-right: 50px;
        cursor: pointer; /* 設置光標的形狀爲爪子 */
    }

    .header .content .logo img {
        vertical-align: middle;
    }

    .header .nav li {
        float: left;
        height: 80px;
        line-height: 80px;
        margin-right: 30px;
        font-size: 16px;
        color: #4a4a4a;
        cursor: pointer;
    }

    .header .nav li span {
        padding-bottom: 16px;
        padding-left: 5px;
        padding-right: 5px;
    }

    .header .nav li span a {
        display: inline-block;
    }

    .header .nav li .this {
        color: #4a4a4a;
        border-bottom: 4px solid #ffc210;
    }

    .header .nav li:hover span {
        color: #000;
    }

    .header .login-bar {
        height: 80px;
    }

    .header .login-bar .shop-cart {
        margin-right: 20px;
        border-radius: 17px;
        background: #f7f7f7;
        cursor: pointer;
        font-size: 14px;
        height: 28px;
        width: 88px;
        margin-top: 30px;
        line-height: 32px;
        text-align: center;
    }

    .header .login-bar .shop-cart:hover {
        background: #f0f0f0;
    }

    .header .login-bar .shop-cart img {
        width: 15px;
        margin-right: 4px;
        margin-left: 6px;
    }

    .header .login-bar .shop-cart span {
        margin-right: 6px;
    }

    .header .login-bar .login-box {
        margin-top: 33px;
    }

    .header .login-bar .login-box span {
        color: #4a4a4a;
        cursor: pointer;
    }

    .header .login-bar .login-box span:hover {
        color: #000000;
    }
</style>
Header.vue

Banner.vue 小組件

<template>
    <el-carousel height="520px" :interval="3000" arrow="always">
        <el-carousel-item>
            <img src="@/assets/img/banner1.png" alt="">
        </el-carousel-item>
        <el-carousel-item>
            <img src="@/assets/img/banner2.png" alt="">
        </el-carousel-item>
        <el-carousel-item>
            <img src="@/assets/img/banner3.png" alt="">
        </el-carousel-item>
    </el-carousel>
</template>
<!---->
<script>
    export default {
        name: "Banner",
    }
</script>

<style scoped>
    .el-carousel__item h3 {
        color: #475669;
        font-size: 18px;
        opacity: 0.75;
        line-height: 300px;
        margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
    }
    img {
        text-align: center;
        height: 520px;
    }
</style>
Banner.vue

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 {
        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>
Footer.vue

global.css 全局樣式

/* 聲明全局樣式和項目的初始化樣式 */
body, h1, h2, h3, h4, 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, li {
    list-style: none;
}

table {
    border-collapse: collapse; /* 合併邊框 */
}

/* 工具的全局樣式 */
.full-left {
    float: left !important;
}

.full-right {
    float: right !important;
}

/*[class*=" el-icon-"], [class^=el-icon-] {
    font-size: 50px;
}*/

.el-carousel__arrow {
    width: 120px;
    height: 120px;
}

.el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background: #ffc210;
    border-color: #ffc210;
    border: none;
}

.el-checkbox__inner:hover {
    border-color: #9b9b9b;
}

.el-checkbox__inner {
    width: 16px;
    height: 16px;
    border: 1px solid #9b9b9b;
    border-radius: 0;
}

.el-checkbox__inner::after {
    height: 9px;
    width: 5px;
}
global.css

 

前端主頁

 

 

7、後臺主頁應用home

一、在dev.py中註冊drf、棄用jdango的csrf

INSTALLED_APPS = [
    'rest_framework',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware',
]

二、建立 home 應用

①前往apps目錄下建立app:cd luffyapi/apps
②建立app:python ../../manage.py startapp home

三、配置 apps 到環境變量並註冊home應用

在dev.py中註冊linux

import os
# 注:BASE_DIR指向的是內層luffyapi  --小路飛
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# 全部的app都放置到 luffyapi/luffyapi/apps,將其添加到環境變量
import sys
sys.path.append(os.path.join(BASE_DIR, 'apps'))

而後再註冊app:homevue-router

INSTALLED_APPS = [
    'home'
]

四、註冊子路由

①主路由

from django.urls import path, include
path('home/', include('home.urls')),

②子路由

from django.urls import path
from . import views
urlpatterns = [
    path('', views.Home.as_view())
]

前端展現

相關文章
相關標籤/搜索