多个链接将参数传递到同一路由

时间:2020-09-21 15:06:25

标签: javascript vue.js vuejs2 vue-router

我有一个“文档”页面,其中包含3个子菜单。

无论单击哪个子菜单,我都想做的是,我希望它转到一个主文档Vue组件,然后将一个变量传递给它,这样子组件也将加载到主组件中。 / p>


这是我到目前为止所拥有的:

导航

<template>
      <div>
          <nav class="navbar navbar-expand-lg">
            <ul class="navbar-nav ml-auto h-100 d-flex align-items-center" style="margin-right: 5rem">
                <div class="nav-item dropdown" @mouseover="openDocDropDown" @mouseleave="closeDocDropDown">
                    <div ref="docDropdown" class="d-flex align-items-center" role="button" data-toggle="dropdown" style="height: 42px; cursor: default">
                        <i class="far fa-file-alt" style="font-size: 1.9rem" />&nbsp;
                        Documentation
                        <i class="ml-2" :class="[docCollapsed ? 'fa-caret-down' : 'fa-caret-up', 'fas']" />
                    </div>
                    <div v-if="!docCollapsed" class="dropdown-menu subMenus" aria-labelledby="navbarDropdown">
                        <router-link to="/documentation" tag="div" class="nav-item d-flex align-items-center p-3" @click.native="docMenuClicked('admin')">
                            <i class="far fa-file-pdf" style="font-size: 1.9rem" />&nbsp;
                            <span class="pt-1 pl-1">Administrator documentation</span>
                        </router-link>
                        <router-link to="/documentation" tag="div" class="nav-item d-flex align-items-center p-3" @click.native="docMenuClicked('reseller_channel')">
                            <i class="far fa-file-pdf" style="font-size: 1.9rem" />&nbsp;
                            <span class="pt-1 pl-1">Reseller/Channel documentation</span>
                        </router-link>
                        <router-link to="/documentation" tag="div" class="nav-item d-flex align-items-center p-3" @click.native="docMenuClicked('cust')">
                            <i class="far fa-file-pdf" style="font-size: 1.9rem" />&nbsp;
                            <span class="pt-1 pl-1">Customer documentation</span>
                        </router-link>
                    </div>
                </div>
                <router-link to="/contactus" tag="li" :class="[currentPage.includes('/conactus/') ? 'router-link-exact-active router-link-active' : '', 'nav-item mainMenuLink d-flex align-items-center']">
                  <i class="d-none d-xl-inline icon-nav_contact_us iconSize" />&nbsp;Contact us
                </router-link>
            </ul>
        </nav>
    </div>
</template>

<script>    
    export default {
        name: "TopNav",

        props: [ 'hasErrored' ],

        data() {
            return {
                unCollapsed: true,
                docCollapsed: true,
                docMenuItemClicked: ''
            }
        },

        computed: {
            ...mapGetters({
                user: 'user'
            }),
          
            currentPage() {
                return this.$route.path;
            }
        },

        methods: {
            openUnDropDown() {
                this.$refs.unDropdown.visible = true;
                this.unCollapsed = false;
            },

            closeUnDropDown() {
                this.$refs.unDropdown.visible = false;
                this.unCollapsed = true;
            },

            openDocDropDown() {
                this.$refs.docDropdown.visible = true;
                this.docCollapsed = false;
            },

            closeDocDropDown() {
                this.$refs.docDropdown.visible = false;
                this.docCollapsed = true;
            },

            docMenuClicked(data) {
                this.docMenuItemClicked = data;

                setTimeout(() => {
                    VueEvent.$emit('document-menu-clicked', { menuClicked:  this.docMenuItemClicked });
                    this.docCollapsed = true;
                }, 1)
            }
        }
    }
</script>

DocumentationMain.vue

<template>
    <div>
        Main Document Page
        {{ menuClicked }}
        <ResellChanDocs v-if="menuClicked === 'reseller_channel'" />
        <CustDocs v-else-if="menuClicked === 'cust'" />
        <AdminDocs v-else />
    </div>
</template>

<script>
import ResellChanDocs from './DocumentationResellerChannel';
import CustDocs from './DocumentationCustomer';
import AdminDocs from './DocumentationAdministrator';

    export default {
        name: 'MainDocumentation',

        components: {
            ResellChanDocs,
            CustDocs,
            AdminDocs
        },

         data() {
            return {
                menuClicked: 'admin'
            }
        },

        beforeMount() {
            VueEvent.$on('document-menu-clicked', (data) => {
                this.menuClicked = data.menuClicked;
            });
        }

    }
</script>

routes.js

import Home from './components/Home.vue';
import ContactUs from './components/ContactUs';
import Documentation from './components/DocumentationMain';

export const routes = [
    { path: '', component: Home },
    { path: '/contactus', component: ContactUs },
    { path: '/documentation', component: Documentation, props: true },
    { path: '*', redirect: '/' }
];

给予

Example screenshot


问题

  1. 有没有更简便的方法?
  2. 当我单击菜单时,我的'DocumenationMain'似乎没有注册VueEvent.$emit
  3. 当我第二次单击链接时,将呈现正确的组件
  4. 当我单击链接时,我所有其他菜单都突出显示为蓝色

我感觉自己做错了

1 个答案:

答案 0 :(得分:0)

只需使用Dynamic Route Matching

export const routes = [
    { path: '', component: Home },
    { path: '/contactus', component: ContactUs },
    { path: '/documentation/:doctype', component: Documentation, props: true },
    { path: '*', redirect: '/' }
];

nav.vue

<router-link to="/documentation/reseller" />
<router-link to="/documentation/cust" />
<router-link to="/documentation/admin" />

Documentation.vue

props: {
  doctype: {
    type: String,
    default: 'admin'
  }
}

路由器现在应该将:doctype值(来自URL)传递到文档组件doctype prop中-使用它来确定要在内部激活的子组件...