feat: 侧边动态路由菜单支持参数

This commit is contained in:
郝先瑞
2024-05-24 18:17:01 +08:00
parent 1c4326fdfa
commit c5f7e4b081
4 changed files with 39 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
<template>
<component :is="type" v-bind="linkProps(to)">
<component :is="linkType" v-bind="linkProps(to)">
<slot></slot>
</component>
</template>
@@ -14,20 +14,25 @@ import { isExternal } from "@/utils/index";
const props = defineProps({
to: {
type: String,
type: Object,
required: true,
},
});
const isExternalLink = computed(() => isExternal(props.to));
const type = computed(() => {
return isExternalLink.value ? "a" : "router-link";
const isExternalLink = computed(() => {
return isExternal(props.to.path || "");
});
const linkProps = (to: string) => {
return isExternalLink.value
? { href: to, target: "_blank", rel: "noopener noreferrer" }
: { to };
const linkType = computed(() => (isExternalLink.value ? "a" : "router-link"));
const linkProps = (to: any) => {
if (isExternalLink.value) {
return {
href: to.path,
target: "_blank",
rel: "noopener noreferrer",
};
}
return { to: to };
};
</script>

View File

@@ -8,7 +8,13 @@
!item.meta?.alwaysShow
"
>
<AppLink v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
<AppLink
v-if="onlyOneChild.meta"
:to="{
path: resolvePath(onlyOneChild.path),
query: onlyOneChild.meta.params,
}"
>
<el-menu-item
:index="resolvePath(onlyOneChild.path)"
:class="{ 'submenu-title-noDropdown': !isNest }"

16
src/views/demo/other.vue Normal file
View File

@@ -0,0 +1,16 @@
<template>
<div>路由参数{{ query }}</div>
</template>
<script setup lang="ts">
defineOptions({
name: "Other",
inheritAttrs: false,
});
import { useRoute } from "vue-router";
// 获取query参数
const query = useRoute().query.type as string;
</script>
<style lang="scss" scoped></style>