Vue開發(fā)基礎(chǔ)二:vue3頁面與組件開發(fā)

一、項目文件目錄結(jié)構(gòu)

mycli項目目錄結(jié)構(gòu)
目錄/文件 說明
node_modules npm 加載的項目依賴模塊
public 公共資源目錄
src/assets 靜態(tài)資源目錄,如圖片、css、js等
src/components 項目公共組件目錄
src/router 路由管理目錄
src/store vuex狀態(tài)管理目錄
src/views 頁面文件 (.vue)
src/App.vue 根組件文件
src/main.js 項目核心入口文件
package.json 項目信息管理及依賴包管理文件
vue.config.js 項目配置文件

二、修改根組件文件 App.vue

1)刪除自動生成的 nav 布局,及其相關(guān)樣式
2)僅留下 router-view 標簽,該標簽自動匹配默認主頁對應(yīng)的路由頁面

  • App.vue
<template>
  <router-view/>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

三、新建頁面及配置路由

(1)新建頁面文件

在 src/views 目錄下新建三個頁面文件,可刪除自動生成的 HomeView.vue 及 AboutView.vue 文件

  • LolView.vue
<template>
  <div>
    <h1>英雄聯(lián)盟</h1>
  </div>
</template>
  • MobaView.vue
<template>
  <div>
    <h1>王者榮耀</h1>
  </div>
</template>
  • MineView.vue
<template>
  <div>
    <h1>我的</h1>
  </div>
</template>

(2)配置路由

在 src/router/index.js 文件中配置路由,設(shè)置LolView為默認首頁

import { createRouter, createWebHistory } from 'vue-router'
import LolView from '../views/LolView.vue'

const routes = [
  {
    path: '/',
    name: 'lol',
    component: LolView
  },
  {
    path: '/moba',
    name: 'moba',
    component: () => import('../views/MobaView.vue')
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/MineView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

(3)訪問地址并測試

默認首頁: http://localhost:8080/
王者榮耀頁面: http://localhost:8080/moba
我的 頁面:http://localhost:8080/mine

三、定義全局樣式

修改根組件文件 App.vue,定義全局樣式內(nèi)容

<style>
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html, body, #app{
  width: 100%;
  height: 100%;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  /* 定義縱向彈性布局 */
  display: flex;
  flex-direction: column;
}
/* 所有子頁面,中間內(nèi)容容器均使用 myContent 樣式 */
.myContent{
  background-color: #ccc;
  flex-grow: 1;
}

四、制作頭部、腳部組件

Vue組件基礎(chǔ) https://cn.vuejs.org/guide/essentials/component-basics.html#using-a-component

在 src/components 文件夾中,新建頁頭及頁腳對應(yīng)的兩個布局文件,可刪除自動生成的 HelloWorld.vue 文件

(1) 創(chuàng)建頁頭組件

  • MyHeader.vue 文件
<template>
    <div class="myHearder">
        <div>{{ strHeader }}</div>
    </div>
</template>

<script setup>
    import { ref } from 'vue';
    const strHeader = ref("WellCome to MyGame !");
</script>

<style>
    .myHearder{
        height: 140px;
        width: 100%;
        display: flex;
        align-items: center;
    }
</style>

(2)創(chuàng)建頁腳組件

  • MyFooter 文件
<template>
    <div class="myFooter">
        <div>MyGame @ 2023</div>
    </div>
</template>

<style>
    .myFooter{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
        height: 100px;
        color: white;
        background-color: #666;
    }
</style>

(3)在頁面中引入組件

在 src/views/LolView.vue 文件中,引入頁頭與頁腳組件

<template>
  <!-- 頁頭組件 -->
  <MyHeader />

  <!-- 中間內(nèi)容容器使用 App.vue 中定義的 myContent 樣式  -->
  <div class="myContent">
    <h1>英雄聯(lián)盟</h1>
  </div>

  <!-- 頁腳組件 -->
  <MyFooter />
</template>

<script setup>
  // 引入頁頭頁腳組件文件
  import MyHeader from '@/components/MyHeader.vue'
  import MyFooter from '@/components/MyFooter.vue'
</script>

(4) 訪問首頁地址測試

  • 每次啟動時,端口號可能不同,需要根據(jù)啟動結(jié)果提示的端口號訪問
http://localhost:8082
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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