一次簡(jiǎn)單的Vue項(xiàng)目

2 搭建一個(gè)簡(jiǎn)單的后臺(tái)管理布局

文件創(chuàng)建

在準(zhǔn)備好相關(guān)環(huán)境后,我們開(kāi)始創(chuàng)建自己的項(xiàng)目。整個(gè)項(xiàng)目的目錄結(jié)構(gòu)如下圖所示

image

我們?cè)趕rc/components/下創(chuàng)建一個(gè)Test.vue文件,這個(gè)文件名可以是任意的修改,相關(guān)代碼如下:

<template>
    <h1>test</h1>
</template>

<script>
export default {
}
</script>

<style>
</style>

然后修改router.ts中的路由信息,有關(guān)路由的進(jìn)階使用后面會(huì)進(jìn)行學(xué)習(xí)。將路由中的根路由修改為為如下:

    {
      path: '/',
      name: 'home',
      component: ()=> import('@/components/Test.vue')
    }

保證main.ts文件中存在<router-view></router-view>標(biāo)簽,這是一個(gè)匹配路由視圖的標(biāo)簽,只有存在這個(gè)標(biāo)簽,才能正常的通過(guò)路由渲染并訪問(wèn)相關(guān)內(nèi)容,最后訪問(wèn)結(jié)果:

image

使用Element UI框架

Element UI框架是一個(gè)基于vue的UI框架,雖然說(shuō)是前端是圖形界面,但是和專業(yè)的UI相比還是差了很多的,因此我們使用框架就行,如果有特殊需求還可以定制主題。

首先是安裝:

npm i element-ui -S

然后在main.ts中修改相關(guān)信息

import Element from 'element-ui'
// 引入element ui
import 'element-ui/lib/theme-chalk/index.css'
// 引入element ui的樣式
Vue.use(Element)

至此完整地引入了element ui的相關(guān)樣式,如有需要可后續(xù)自己手動(dòng)修改。

實(shí)現(xiàn)整體布局

本次使用的是element ui的布局容器,我們直接復(fù)制粘貼源代碼到Test.vue中試試

<template>
  <el-container>
    <el-aside width="200px">Aside</el-aside>
    <el-container>
      <el-header>Header</el-header>
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>

<style>
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
  
  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }
</style>

效果圖如下:

image

但是在實(shí)際的展示過(guò)程中布局并沒(méi)有充滿整個(gè)瀏覽器,我們來(lái)試試如何充滿瀏覽器。

充滿瀏覽器

我們?cè)贏pp.vue中的樣式部分添加如下代碼即可

html,
body,
#app,
.el-container {
  /*設(shè)置內(nèi)部填充為0,幾個(gè)布局元素之間沒(méi)有間距*/
  padding: 0px;
  /*外部間距也是如此設(shè)置*/
  margin: 0px;
  /*統(tǒng)一設(shè)置高度為100%*/
  height: 100%;
}

最終效果:

image

在aside所在的容器里面添加element的NavMenu組件,此處我們?cè)傩陆ㄒ粋€(gè)Main.vue文件,在這個(gè)文件中進(jìn)行NavMenu的編寫(xiě)。為了能夠顯示Main.vue中的內(nèi)容,我們需要在Test.vue中注冊(cè)這個(gè)組件:

<el-aside width="200px">
    <navmenu></navmenu>
    <!--即輸入我們注冊(cè)好的組件的標(biāo)簽-->
</el-aside>
<script>
import Main from "@/components/Main";
export default {
  name: "app",
  components: {
    navmenu: Main
  }
};
</script>

先把官網(wǎng)的組件的源碼copy下來(lái)(html和script部分):

<template>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    @open="handleOpen"
    @close="handleClose"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <el-submenu index="1">
      <template slot="title">
        <i class="el-icon-location"></i>
        <span>導(dǎo)航一</span>
      </template>
      <el-menu-item-group>
        <template slot="title">分組一</template>
        <el-menu-item index="1-1">選項(xiàng)1</el-menu-item>
        <el-menu-item index="1-2">選項(xiàng)2</el-menu-item>
      </el-menu-item-group>
      <el-menu-item-group title="分組2">
        <el-menu-item index="1-3">選項(xiàng)3</el-menu-item>
      </el-menu-item-group>
      <el-submenu index="1-4">
        <template slot="title">選項(xiàng)4</template>
        <el-menu-item index="1-4-1">選項(xiàng)1</el-menu-item>
      </el-submenu>
    </el-submenu>
    <el-menu-item index="2">
      <i class="el-icon-menu"></i>
      <span slot="title">導(dǎo)航二</span>
    </el-menu-item>
    <el-menu-item index="3" disabled>
      <i class="el-icon-document"></i>
      <span slot="title">導(dǎo)航三</span>
    </el-menu-item>
    <el-menu-item index="4">
      <i class="el-icon-setting"></i>
      <span slot="title">導(dǎo)航四</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
export default {
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  }
};
</script>

這里要做一下說(shuō)明的是,我們將官方的源代碼中的<el-col>和<el-row>標(biāo)簽刪掉了。因?yàn)槿绻由线@兩個(gè)標(biāo)簽的話,整個(gè)navmenu的高度將無(wú)法充滿整個(gè)窗口??匆幌滦Ч?/p>

image

下拉子菜單右側(cè)沒(méi)有完全對(duì)齊,存在溢出的情況。而且導(dǎo)航欄的每個(gè)子菜單好像位置有點(diǎn)靠右了。經(jīng)過(guò)檢查元素的樣式發(fā)現(xiàn)是因?yàn)閎order-right組件留出了1px的邊界導(dǎo)致無(wú)法對(duì)齊,然后再按照視覺(jué)要求調(diào)整相應(yīng)的padding-left和padding-right。

<style>
.el-menu {
  height: 100%;
  border-right: solid 0px #e6e6e6;
}
.el-submenu__title,
.el-menu-item {
  height: 60px;
  padding-right: 70px;
  padding-left: 0px !important;
}
.el-menu-item-group__title {
  padding-right: 110px;
}
</style>

最后的效果如下:

image

此次的頁(yè)面布局就介紹到這里,下一節(jié)將會(huì)介紹路由的相關(guān)使用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 3 路由實(shí)現(xiàn) 在摸爬滾打一段時(shí)間后,決定進(jìn)行路由相關(guān)的處理。 利用導(dǎo)航欄進(jìn)行路由 本文采用的是element ui...
    Prejudice_ccca閱讀 641評(píng)論 0 1
  • 5 登錄驗(yàn)證及權(quán)限設(shè)置 既然要做一個(gè)系統(tǒng),肯定就少不了登錄驗(yàn)證,我們先實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄頁(yè)面。首先在compone...
    Prejudice_ccca閱讀 3,074評(píng)論 2 1
  • 4 表格使用 表格是前端經(jīng)常使用到的一個(gè)工具,尤其是在管理系統(tǒng)中,因?yàn)榇嬖谥鴶?shù)據(jù)的展示和操作(增刪改查)。 我們將...
    Prejudice_ccca閱讀 505評(píng)論 0 0
  • 1 環(huán)境準(zhǔn)備 最近由于項(xiàng)目需要且人員不足(其實(shí)是不想求別人。。。),又希望多學(xué)點(diǎn)知識(shí),于是動(dòng)手自己搭建一個(gè)后臺(tái)管理...
    Prejudice_ccca閱讀 995評(píng)論 0 1
  • 基于Vue的一些資料 內(nèi)容 UI組件 開(kāi)發(fā)框架 實(shí)用庫(kù) 服務(wù)端 輔助工具 應(yīng)用實(shí)例 Demo示例 element★...
    嘗了又嘗閱讀 1,282評(píng)論 0 1

友情鏈接更多精彩內(nèi)容