最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3點擊側(cè)邊導航欄完成切換頁面內(nèi)組件全過程(WEB)

 更新時間:2023年06月12日 11:37:46   作者:li-xun  
寫頁面的時候都會用到一些導航欄、點擊不同的部分切換不同的頁面,下面這篇文章主要給大家介紹了關(guān)于Vue3點擊側(cè)邊導航欄完成切換頁面內(nèi)組件的相關(guān)資料,需要的朋友可以參考下

效果

點擊左側(cè)導航,右面頁面切換。

思路

使用router-view顯示點擊后需要切換的組件,需要先完成網(wǎng)頁的結(jié)構(gòu)。側(cè)面導航 + 頁面顯示部分

如:

導航加顯示

設(shè)置一個class屬性,點擊元素時給當前元素額外添加一個class類。給導航中每個點擊項添加上點擊事件,當點擊當前項時其它項的class變成原本的,當前元素添加一個當前點擊的class類,而組件切換則由router完成,使用router-link to 完成路由路徑切換。

當前點擊模式類代碼:

.isclick {
		background: #e9feff;
		color: #0bbfbc;
	}

過程

獲取當前點擊DOM并添加點擊class

這里需要注意,觸發(fā)事件的元素對象(不一定是綁定事件的對象,會因為事件冒泡變化),所以我們需要獲取綁定事件的元素對象。

  • $event:當前觸發(fā)的是什么事件
  • $event.target:觸發(fā)事件的元素對象(不一定是綁定事件的對象)
  • $event.currentTarget:綁定事件的元素對象

此處需要使用: e.currentTarget

改變選中元素的class: e.currentTarget.className = ‘nav-item isclick’

其中nav-item為未點擊時的navitem樣式類,isclick為點擊后更改的樣式類,原來樣式只有一個nav-item,再添加一個isclick。

將其它的導航未點擊項isclick樣式類去除

得到點擊項的其它兄弟節(jié)點

var domList = e.currentTarget.parentNode.children

parentNode 是得到父節(jié)點

children 是得到子節(jié)點

當前元素的父節(jié)點的所有子節(jié)點,是一個列表

將所有節(jié)點的點擊樣式類去除

for(let d=0;d<domList.length;d++){
	domList[d].className = 'nav-item'
}

遍歷當前所有節(jié)點的兄弟節(jié)點,并修改class。

完整代碼

導航代碼

導航組件代碼:

<template>
	<div class="nav">
		<ul>
			<li v-for="item in NavList" class="nav-item" @click="clickNav($event)">
				<div>
					<img class="nav-icon" :src="item.icon" width="20px" height="20px" alt="sy" />
					<router-link :to="item.url">{{item.title}}</router-link>
				</div>
			</li>
		</ul>
		<div class="nav-img-box" style="margin-top: 108px;">
			<img src="@/assets/裝飾圖.png" alt="zs" />
		</div>
	</div>
</template>
<script setup>
import { ref,reactive } from 'vue'
var NavList = reactive([
	{
		title: "合作伙伴",
		url: "/partner",
		icon: require("@/assets/首頁-選中.png")
	},
	{
		title: "客戶列表",
		url: "/customer",
		icon: require("@/assets/客戶列表.png")
	},
	{
		title: "訂單列表",
		url: "/order",
		icon: require("@/assets/財務(wù)列表.png")
	},
	{
		title: "物料列表",
		url: "/materials",
		icon: require("@/assets/標簽列表.png")
	},
	{
		title: "成員管理",
		url: "/corpuser",
		icon: require("@/assets/員工列表.png")
	},
	{
		title: "收益概覽",
		url: "/income",
		icon: require("@/assets/員工列表.png")
	},
	{
		title: "價格配置",
		url: "/price",
		icon: require("@/assets/員工列表.png")
	},
	{
		title: "系統(tǒng)設(shè)置",
		url: "/setting",
		icon: require("@/assets/員工列表.png")
	}
])
function clickNav(e) {
	var domList = e.currentTarget.parentNode.children
	for(let d=0;d<domList.length;d++){
		domList[d].className = 'nav-item'
	}
	console.log('點擊',e.currentTarget)
	e.currentTarget.className = 'nav-item isclick'
}
</script>
<style scoped>
	li {
		list-style-type: none;
	}
	a {
		text-decoration: none;
		color: #39475A;
	}
	.nav {
		width: 200px;
		height: 100%;
		background: #FFFFFF;
		box-shadow: 3px 0px 6px 0px rgba(82, 82, 82, 0.14);
	}
	.nav ul {
		margin-top: 32px;
		padding: 0;
	}
	.nav-item {
		width: 200px;
		height: 46px;
		font-family: PingFangSC-Medium;
		font-size: 16px;
		color: #39475A;
		letter-spacing: 0;
		line-height: 46px;
		font-weight: 500;
	}
	.nav-item div {
		margin-left: 17px;
		height: 46px;
		line-height: 46px;
	}
	.nav-item div img {
		width: 20px;
		height: 20px;
	}
	.nav-icon {
		vertical-align: middle;
		margin-right: 7px;
		padding-bottom: 6px;
	}
	.isclick {
		background: #e9feff;
		color: #0bbfbc;
	}
	.nav-img-box img {
		width: 200px;
		height: 416px;
	}
</style>

代碼中的icon: require(“@/assets/員工列表.png”)為導航圖標,需要注意,如果不用require,直接寫圖片的地址的話可能出現(xiàn)無法找到圖片的問題,圖片的地址在html中使用時會被轉(zhuǎn)碼,出現(xiàn)類似%E9%A6%96%E9%A1%B5-%E9%80%89%E4%B8%AD.png的情況。

顯示頁面代碼

<template>
  <div class="page-body">
      <PageNav></PageNav>
      <router-view />
  </div>
  <div class="bottom">
    <PageBottom></PageBottom>
  </div>
</template>
<script setup>
import PageNav from "@/components/PageNav"
import PartnerView from "@/views/PartnerView"
import PageBottom from "@/components/PageBottom.vue"
</script>
<style scoped>
.page-body {
  display: flex;
}
</style>

其中PageNav為前面的導航代碼

路由設(shè)置

router.js中代碼如下

import { createRouter, createWebHistory } from 'vue-router'
import PartnerView from '../views/PartnerView.vue'
import CustomerView from '../views/CustomerView.vue'
import OrderView from '@/views/OrderView'
import MaterialsView from '../views/MaterialsView'
import CorpUserView from '@/views/CorpUserView'
import InComeView from '@/views/InComeView'
import PriceView from '@/views/PriceView'
import SettingView from '@/views/SettingView'
const routes = [
  {
    path: '/',
    name: 'index',
    component: PartnerView,
    redirect: 'partner',
  },
  {
    path: '/partner',
    name: 'partner',
    component: PartnerView
  },
  {
    path: '/customer',
    name: 'customer',
    component: CustomerView
  },
  {
    path: '/order',
    name: 'order',
    component: OrderView
  },
  {
    path: '/materials',
    name: 'materials',
    component: MaterialsView
  },
  {
    path: '/corpuser',
    name: 'corpuser',
    component: CorpUserView
  },
  {
    path: '/income',
    name: 'income',
    component: InComeView
  },
  {
    path: '/price',
    name: 'price',
    component: PriceView
  },
  {
    path: '/setting',
    name: 'setting',
    component: SettingView
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
export default router

總結(jié)

到此這篇關(guān)于Vue3點擊側(cè)邊導航欄完成切換頁面內(nèi)組件的文章就介紹到這了,更多相關(guān)Vue3切換頁面內(nèi)組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3實現(xiàn)ai聊天對話框功能

    vue3實現(xiàn)ai聊天對話框功能

    這篇文章主要介紹了vue3實現(xiàn)ai聊天對話框功能,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-12-12
  • vue長按事件touch示例詳解

    vue長按事件touch示例詳解

    這篇文章主要介紹了vue長按事件touch,文末給大家補充介紹了Vue長按觸摸事件的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Vue.js實現(xiàn)高質(zhì)量翻頁功能的完整開發(fā)指南

    Vue.js實現(xiàn)高質(zhì)量翻頁功能的完整開發(fā)指南

    當我們在網(wǎng)頁中展示大量數(shù)據(jù)時,分頁能幫助用戶快速瀏覽內(nèi)容,提高頁面的加載性能和用戶體驗,本文將從基礎(chǔ)翻頁功能入手,逐步升級至可復(fù)用的分頁組件,并提供性能優(yōu)化和用戶體驗提升的實用建議,需要的朋友可以參考下
    2025-07-07
  • vue3.0 自適應(yīng)不同分辨率電腦的操作

    vue3.0 自適應(yīng)不同分辨率電腦的操作

    這篇文章主要介紹了vue3.0 自適應(yīng)不同分辨率電腦的操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • vue?this.$router和this.$route區(qū)別解析及路由傳參的2種方式?&&?this.$route的各種語法

    vue?this.$router和this.$route區(qū)別解析及路由傳參的2種方式?&&?this.$route

    this.$router?相當于一個全局的路由器對象,包含了很多屬性和對象(比如?history?對象),任何頁面都可以調(diào)用其?push(),?replace(),?go()?等方法,本文給大家介紹Vue中this.$router與this.$route的區(qū)別?及push()方法,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • 詳解基于vue-cli配置移動端自適應(yīng)

    詳解基于vue-cli配置移動端自適應(yīng)

    本篇文章主要介紹了詳解基于vue-cli配置移動端自適應(yīng),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 解決iView Table組件寬度只變大不變小的問題

    解決iView Table組件寬度只變大不變小的問題

    這篇文章主要介紹了解決iView Table組件寬度只變大不變小的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue.config.js中的devServer使用

    vue.config.js中的devServer使用

    這篇文章主要介紹了vue.config.js中的devServer使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 解決Antd輸入框卡頓問題以及Pubsub.js的使用方式

    解決Antd輸入框卡頓問題以及Pubsub.js的使用方式

    這篇文章主要介紹了解決Antd輸入框卡頓問題以及Pubsub.js的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue 2源碼解析HTMLParserOptions.start函數(shù)方法

    Vue 2源碼解析HTMLParserOptions.start函數(shù)方法

    這篇文章主要為大家介紹了Vue 2源碼解析HTMLParserOptions.start函數(shù)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論

亚东县| 郎溪县| 乌拉特中旗| 周宁县| 河间市| 墨江| 保德县| 娄烦县| 威宁| 白山市| 历史| 田东县| 大城县| 宝应县| 灵武市| 大庆市| 英吉沙县| 马公市| 师宗县| 顺义区| 郸城县| 临武县| 横峰县| 清苑县| 平罗县| 岢岚县| 电白县| 原阳县| 临高县| 柳江县| 前郭尔| 宾川县| 青铜峡市| 左云县| 江源县| 当涂县| 沛县| 福州市| 孟津县| 阜阳市| 牙克石市|