1. 程式人生 > 其它 >laravel+vue專案實現動態路由標籤

laravel+vue專案實現動態路由標籤

技術標籤:簡單記錄vuelaraveljavascript

簡單記錄一下laravel+vue專案中使用vue路由實現的動態標籤

直接上一個真實的操作效果吧,只是實現了基本功能,沒有去佈局
在這裡插入圖片描述
實現動態路由是直接對vue的路由直接進行操作,然後跟換元件頁面來實現的。

js程式碼

RouteTag.vue
<template>
    <div class="tags" v-if="showTags">
        //顯示當前路由標籤
        <ul>
            <li class
="tags-li" v-for="(item,index) in tagsList" :class="{'active': isActive(item.path)}" :key="index"> <router-link :to="item.path" class="tags-li-title"> {{item.title}} </router-link>
//這裡判斷index是為了讓預設存在的路由標籤不會被關閉 <span class="tags-li-icon" @click="closeTags(index)" v-if="index"><i class="el-icon-close"></i></span> </li> </ul> //關閉其他和關閉所有路由標籤的選項 <
div class="tags-close-box"> <el-dropdown @command="handleTags"> <el-button size="mini"> <i class="el-icon-arrow-down el-icon--right"></i> </el-button> <el-dropdown-menu size="small" slot="dropdown"> <el-dropdown-item command="other">關閉其他</el-dropdown-item> <el-dropdown-item command="all">關閉所有</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </div> </div> </template> <script> export default { name: "RouteTag", data() { return { //裝每個路由標籤屬性的陣列 tagsList: [], } }, computed: { showTags() { return this.tagsList.length > 0; } }, watch: { // 對router進行監聽,每當訪問router時,對tags的進行修改 $route(newValue) { this.setTags(newValue); } }, created() { // 第一次進入頁面時,修改tag的值 this.tagsList.push({ title: 'home', path: '/', }) }, methods: { isActive(path) { return path === this.$route.fullPath; }, // 關閉單個標籤 closeTags(index) { const delItem = this.tagsList.splice(index, 1)[0]; const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1]; if (item) { delItem.path === this.$route.fullPath && this.$router.push(item.path); } else { this.$router.push('/'); this.show=false; } }, // 關閉全部標籤 closeAll() { //過濾出首頁標籤,並跳轉到首頁去 const curItem = this.tagsList.filter(item => { return item.path === '/'; }) this.tagsList = curItem; this.$router.push('/'); }, // 關閉其他標籤 closeOther() { //過濾出首頁和當前路由標籤 const curItem = this.tagsList.filter(item => { console.log(item.path) return (item.path === this.$route.fullPath || item.path === '/'); }) this.tagsList = curItem; }, // 設定標籤 setTags(route) { //用some判斷當前監聽路由路徑是否是tagsList其中一個 //some如果回撥函式中有任意一個滿足條件,則返回true;否則為false。 const isExist = this.tagsList.some(item => { return item.path === route.fullPath; }) //如果tagsList其中沒有就新增進去 if(!isExist && this.tagsList) { this.tagsList.push({ title: route.meta.title, path: route.fullPath, name: route.matched[0].components.default.name }) } }, // 當關閉所有頁面時隱藏 handleTags(command) { command === 'other' ? this.closeOther() : this.closeAll(); } } } </script> <style scoped> .tags { position: relative; height: 50px; overflow: hidden; background: white; padding-right: 120px; } .tags ul { box-sizing: border-box; width: 100%; height: 100%; } .tags-li { float: left; margin: 3px 5px 2px 3px; border-radius: 3px; font-size: 15px; overflow: hidden; cursor: pointer; height: 30px; line-height: 23px; border: 1px solid #e9eaec; background: #fff; padding: 0 5px 0 12px; vertical-align: middle; color: #666; -webkit-transition: all .3s ease-in; -moz-transition: all .3s ease-in; transition: all .3s ease-in; } .tags-li:not(.active):hover { background: #f8f8f8; } .tags-li-title { float: left; max-width: 80px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; margin-right: 5px; color: #666; } .tags-li.active { color: #fff; border: 1px solid #10B9D3; background-color: #67C23A; } .tags-li.active .tags-li-title { color: #fff; } .tags-close-box { position: fixed; right: 0; top: 50px; box-sizing: border-box; padding-top: 1px; text-align: center; z-index: 10; } </style>

這裡給tagsList push進去的資料是你在路由檔案定義好的屬性,下面是我定義的路由檔案:

{
            path: '/',
            name: 'home',
            component: resolve => void(require(['../components/Head/Home.vue'], resolve)),
            meta: {title: 'home'}
        },
        {
            path: '/school',
            name: 'school',
            component: resolve => void(require(['../components/Head/School.vue'], resolve)),
            meta: {title: 'school'}
        },
        {
            path: '/course',
            name: 'course',
            component: resolve => void(require(['../components/Head/Course.vue'], resolve)),
            meta: {title: 'course'}
        }

然後直接在需要使用路由標籤的地方直接使用RouteTag.vue就行了

	<RouteTag></RouteTag>
	import RouteTag from "./components/RouteTag";
	components: {
            RouteTag,
        },