场景:左侧菜单栏的菜单项有内部组件切换,也会有点击后进入外链的情况,如何同时处理这种情况?
解决思路:
路由配置:
- {
- // 'path': '/activity',
- 'path': 'https:www.baidu.com',
- 'name': 'activity',
- 'component': Activity
- }
AppLink封装:插槽形式显示el-menu-item
-
-
-
- <component v-bind="isExternalLink(to)">
- <slot>slot>
- component>
- <script>
- import { Validator } from "@bigbighu/cms-utils";
- export default {
- name: "AppLink",
- props: {
- to: {
- type: String,
- required: true
- }
- },
- methods: {
- isExternalLink(url) {
- // /^(https?:|mailto:|tel:)/.test(path)
- if (Validator.isExternal(url)) {
- return {
- is: "a",
- href: url,
- target: "_blank",
- rel: "noopener"
- };
- }
- return {
- is: "router-link",
- to: url
- };
- }
- }
- };
- script>
el-menu-item使用:
-
- <app-link v-if="child.redirect != 'noRedirect' && child.meta" :to="resolvePath(child.path)">
- <el-menu-item v-if="child && child.meta" :index="resolvePath(child.path)">
- <i class="el-icon-setting">i>
- <span slot="title">{{child.meta.title }}span>
- el-menu-item>
- app-link>