通過(guò)vue刷新左側(cè)菜單欄操作
今天完成了手頭任務(wù)就想著做點(diǎn)什么,剛好領(lǐng)導(dǎo)讓我看看項(xiàng)目左側(cè)菜單欄不刷新的問(wèn)題,我也是剛剛接觸vue,很多東西都還不是很熟練,這也是我的第一篇自己寫的博客,感覺(jué)還是很興奮的,我覺(jué)得寫博客這個(gè)習(xí)慣要一直養(yǎng)成,不但總結(jié)了自己一天的工作所得,而且也是對(duì)自己的一種良好習(xí)慣的養(yǎng)成。
下面進(jìn)入正題。
這個(gè)是我們html里面的超鏈接,而我們的點(diǎn)擊事件的跳轉(zhuǎn)就是通過(guò)這個(gè)超鏈接實(shí)現(xiàn)的。
<el-menu-item index="3-1"><a href="#/commodity-list" rel="external nofollow" >
然后我們要?jiǎng)?chuàng)建一個(gè)js文件,將我們要跳轉(zhuǎn)的路徑導(dǎo)入
import ChannelList from './src/commodity-manage/channel-list/channel-list'
配置路由管理:
const router = new VueRouter({
routes: [
{
path: '/commodity-list',
name: 'commodity-list',
component: commodityStorage,
children: []
}
]
path:就是我們要跳轉(zhuǎn)的路徑
name:跳轉(zhuǎn)文件的名字
component:配置了映射的組件
在html文件中配置了<router-view/>
<router-view :key="key"></router-view>
是用來(lái)渲染通過(guò)路由映射過(guò)來(lái)的組件,當(dāng)路徑更改時(shí),<router-view> 中的內(nèi)容也會(huì)發(fā)生更改
在js文件中使用computed來(lái)進(jìn)行監(jiān)聽(tīng)
//每次讓路由生成不同的值,用于重新加載組件,達(dá)到刷新數(shù)據(jù)的效果
computed: {
key() {
return this.$route.name !== undefined? this.$route.name +new Date(): this.$route +new Date()
}
},
補(bǔ)充知識(shí):vue:路由菜單(element 和 antd)
在 vue 中 使用 UI框架中的菜單,給菜單如何添加路由呢?其中會(huì)出現(xiàn)路由樣式的問(wèn)題。請(qǐng)看下面兩種UI方法。
注)使用框架的時(shí)候注入知道的吧。。。。。防止有些人xxxx,我還是寫一下。

場(chǎng)景:使用 elementUI 的 NavMenu 時(shí)。
這里請(qǐng)注意:可以不使用 router-link,在 e-menu 上面綁定 route 或者 :route = 'true' ,然后遍歷的時(shí)候 :index=‘route.path' (:index=‘路徑')。

代碼
<template>
<div class="menu">
<el-menu default-active='activePath'
router
@open='handleOpen'
@close='handleClose'
background-color='#545c64'
text-color='#fff'
active-text-color='#ffd04b' >
<template v-for="(route,index) in routes">
<!-- 一級(jí)菜單 -->
<el-menu-item :key='index' v-if='route.children && route.children.length== 1' :index='route.path'>
<i :class="'el-icon-' + route.meta.icon"></i>
<span>{{route.meta.title}}</span>
</el-menu-item>
<!-- 二級(jí)菜單 -->
<el-submenu v-if='route.children && route.children.length > 1' :key='index' :index='route.path'>
<template slot='title'>
<i :class="'el-icon-' + route.meta.icon"></i>
{{route.meta.title}}
</template>
<el-menu-item-group v-for='(item, index) in route.children'>
<el-menu-item :key='index' :index='resolve(route.path, item.path)'>
<i :class="'el-icon-' + item.meta.icon"></i>
{{item.meta.title}}
</el-menu-item>
</el-menu-item-group>
</el-submenu>
</template>
</el-menu>
</div>
</template>
<script>
export default {
name: 'Menu',
data() {
return {
activePath: this.$router.path,
}
},
computed: { // 計(jì)算屬性:獲取路由
routes() {
console.log('test', this.$router)
console.log('ddd', this.$router.options.routes)
return this.$router.options.routes
},
},
methods: {
resolve(p,i){
return `${p}/${i}`
},
handleOpen(key, keyPath) {
console.log(key, keyPath);
},
handleClose(key, keyPath) {
console.log(key, keyPath);
}
},
}
</script>
<style lang='less'>
.el-menu {
text-align: left;
}
</style>
場(chǎng)景:使用 antd 的 Menu 時(shí)。
這個(gè)里面是需要使用route-link做路由跳轉(zhuǎn)的。

代碼
<template>
<div class="menu">
<a-menu v-model="current" mode="inline" theme="dark">
<template v-for='route in routes'>
<!-- 一級(jí)菜單 -->
<a-menu-item v-if='route.children && route.children.length == 1' :key='route.path'>
<router-link :to='route.path'>
<a-icon :type='route.meta.icon' />
{{ route.meta.title }}
</router-link>
</a-menu-item>
<!-- 二級(jí)菜單 -->
<a-sub-menu v-else='route.children && route.children.length == 2' key="sub1">
<span slot="title"><span><a-icon :type='route.meta.icon' />{{ route.meta.title}}</span></span>
<a-menu-item v-for='item in route.children' :key='item.path'>
<router-link :to='resolve(route.path,item.path)'>
<!-- <router-link :to="`${route.path}/${item.path}`"> -->
<a-icon :type='item.meta.icon' />
{{ item.meta.title }}
</router-link>
</a-menu-item>
</a-sub-menu>
</template>
</a-menu>
</div>
</template>
<script>
export default {
name: 'Menu',
data() {
return {
current: ['/'],
}
},
computed: { // 計(jì)算屬性:獲取路由
routes() {
console.log('test', this.$router)
console.log('ddd', this.$router.options.routes)
return this.$router.options.routes
},
},
methods:{
resolve(p,i){
return `${p}/${i}`
},
},
}
</script>
以上這篇通過(guò)vue刷新左側(cè)菜單欄操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于vue-cli3創(chuàng)建libs庫(kù)的實(shí)現(xiàn)方法
這篇文章主要介紹了基于vue-cli3創(chuàng)建libs庫(kù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開(kāi)的問(wèn)題
今天小編就為大家分享一篇解決Vue-cli npm run build生產(chǎn)環(huán)境打包,本地不能打開(kāi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue.js引用背景圖background無(wú)效的3種解決方案
這篇文章主要介紹了vue.js引用背景圖background無(wú)效的3種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁(yè)功能(附demo)
這篇文章主要介紹了vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁(yè)功能(附demo),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03
vue中父子組件注意事項(xiàng),傳值及slot應(yīng)用技巧
這篇文章主要介紹了vue中父子組件注意事項(xiàng),傳值及slot應(yīng)用技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Vue.js彈出模態(tài)框組件開(kāi)發(fā)的示例代碼
本篇文章主要介紹了Vue.js彈出模態(tài)框組件開(kāi)發(fā)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
vue實(shí)現(xiàn)iview表格添加篩選功能的示例代碼
本文主要介紹了vue實(shí)現(xiàn)iview表格添加篩選功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
vue對(duì)象或者數(shù)組中數(shù)據(jù)變化但是視圖沒(méi)有更新問(wèn)題及解決
這篇文章主要介紹了vue對(duì)象或者數(shù)組中數(shù)據(jù)變化但是視圖沒(méi)有更新問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

