【VUE】Vue2菜单搜索并跳转指定页面

有时候后台管理系统复杂的菜单结构与更深的层级使用户无法快速找到自己所需的页面,如何提高后台管理系统的便捷度并且使系统与用户的交互更加灵活成为了项目在基础功能之外更重要的追求。

菜单目录支持搜索并且可直接跳转便可以达到以上诉求。

  • 适用于 Vue2
  • 基于 ruoyi
  • 使用elementUI
  • 最多支持四层,可根据需要自行添加或者删除循环以增加或减少层级

<template>
	<div class="search">
		<!-- 搜索框 带放大镜图标 -->
		<el-button icon="el-icon-search" @click="clickBtn">菜单搜索</el-button>

		<el-dialog
			width="600px"
			class="routerSelect_dialog"
			:visible.sync="isShowSearch"
			:show-close="false"
			:append-to-body="true"
		>
			<el-select
				style="width: 100%"
				ref="headerSearchSelect"
				v-model="searchValue"
				:remote-method="query"
				filterable
				default-first-option
				remote
				placeholder="请输入菜单名"
				class="header_search_select"
				@change="goPath"
			>
				<el-option
					v-for="(item, index) in routerList"
					:key="index"
					:value="item.path"
					:label="item.title"
				>
					<!-- 菜单图标 -->
					<svg-icon :icon-class="item.icon" style="margin: 5px 5px 0 0" />
					<!-- 菜单名称 -->
					{{ item.title }}
				</el-option>
			</el-select>
		</el-dialog>
	</div>
</template>

<script>
import { mapGetters } from 'vuex';

export default {
	name: 'menuSearch',
	data() {
		return {
			searchValue: '',
			isShowSearch: false,
			menu: [],
			routerList: [],
		};
	},
	computed: {
		...mapGetters(['sidebarRouters']),
	},
	mounted() {
		this.getRouterList();
	},

	methods: {
		// 点击搜索按钮
		clickBtn() {
			this.isShowSearch = true;
			setTimeout(() => {
				// 点击搜索后默认弹出下拉框进行选择
				this.$refs.headerSearchSelect.focus();
			}, 500);
		},
		// 选中菜单后跳转到指定页面
		goPath(val) {
			this.$router.push(val);
			this.isShowSearch = false;
			this.searchValue = '';
		},
		// 处理路由信息 - 无标题、重定向、无子菜单项不处理
		getRouterList() {
			this.menu = this.sidebarRouters;
			for (let item1 of this.menu) {
				if (!item1.hidden && item1.children && item1.redirect === 'noRedirect') {
					for (let item2 of item1.children) {
						if (!item2.hidden && item2.children) {
							for (let item3 of item2.children) {
								if (!item3.hidden && item3.children) {
									for (let item4 of item3.children) {
										this.routerList.push({
											title:
												item1.meta.title +
												' > ' +
												item2.meta.title +
												' > ' +
												item3.meta.title +
												' > ' +
												item4.meta.title,
											path:
												item1.path.slice(1) +
												'/' +
												item2.path +
												'/' +
												item3.path +
												'/' +
												item4.path,
											icon: item1.meta.icon,
										});
									}
								} else if (!item3.hidden) {
									this.routerList.push({
										title:
											item1.meta.title +
											' > ' +
											item2.meta.title +
											' > ' +
											item3.meta.title,
										path:
											item1.path.slice(1) + '/' + item2.path + '/' + item3.path,
										icon: item1.meta.icon,
									});
								}
							}
						} else {
							this.routerList.push({
								title: item1.meta.title + ' > ' + item2.meta.title,
								path: item1.path.slice(1) + '/' + item2.path,
								icon: item1.meta.icon,
							});
						}
					}
				}
			}
		},
	},
};
</script>

<style lang="scss">
.search {
	height: 100%;

	button {
		height: 30px;
		display: flex;
		justify-content: center;
		align-items: center;
	}
}
.routerSelect_dialog {
	background: rgba(33, 85, 163, 0.16);
	backdrop-filter: blur(5px);
	.el-dialog {
		height: 35px;
		overflow: hidden;
		border-radius: 24px;
		background: rgba(1, 1, 1, 0);
		margin-top: 40vh !important; //搜索框居中
		.el-dialog__header {
			display: none;
		}
		.el-dialog__body {
			padding: 0 !important;
			margin-top: 0 !important;
		}
		.el-input,
		.is-focus {
			input {
				border: none;
			}
		}
	}
}
</style>

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 中通过导航菜单跳转页面可以使用 Vue Router 来实现。 首先,需要在目中安装 Vue Router: ``` npm install vue-router --save ``` 然后在 `main.js` 中引入并使用 Vue Router: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' import Home from './views/Home.vue' import About from './views/About.vue' Vue.use(VueRouter) const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = new VueRouter({ routes // short for `routes: routes` }) new Vue({ router, render: h => h(App), }).$mount('#app') ``` 在上面的代码中,我们定义了两个路由,分别是 `/` 和 `/about`,并分别对应着 `Home` 和 `About` 组件。然后通过 `VueRouter` 创建了一个路由实例,并将其传递给 `Vue` 实例中。 接下来,在目中创建导航菜单,在导航菜单使用 `router-link` 组件来实现页面跳转: ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` 在上面的代码中,我们通过 `to` 属性来指定跳转的路径。当用户点击导航菜单中的链接时,路由会根据路径自动渲染对应的组件。 除了使用 `router-link` 组件,我们还可以使用编程式导航来实现页面跳转。例如,在组件中可以使用 `$router.push` 方法来进行跳转: ```javascript // 在组件中跳转到 /about 页面 this.$router.push('/about') ``` 以上就是在 Vue 中通过导航菜单跳转页面的基本实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值