1

i'm trying to make my content component dynamic.(using vue-router)

this is my code:

<template>
    <div class="row mb-3" id="customer-panel">
        <Breadcrumb></Breadcrumb>
        <CustomerSidebar></CustomerSidebar>
        <div class="col-lg-9">
            <PersonalInfo></PersonalInfo>
        </div>
    </div>
</template>

any thing is fixed in all pages except PersonalInfo component.

and this is my route:

{
        path: '/customer',
        component: Profile,
        redirect: '/customer/profile',
        children: [
            {
                path: 'profile',
                name: 'profile',
                component: Profile
            },
            {
                path: 'order',
                name: 'order',
                component: Order
            },
        ]
    }

what should i do?

i don't want to repeat my code and copy & past in another component. i just want when i go to order route, Order component loaded instead of PersonalInfo.

what is the best way?

2 Answers 2

2

https://router.vuejs.org/guide/essentials/nested-routes.html

You can acheive your desired result with <router-view></router-view>

Any nested routes / children of your parent route will be rendered here, on route change.

Component:

<template>
    <div class="row mb-3" id="customer-panel">
        <Breadcrumb></Breadcrumb>
        <CustomerSidebar></CustomerSidebar>
        <div class="col-lg-9">

            <router-view></router-view>

        </div>
    </div>
</template>

Routes:

{
        path: '/customer',
        component: Customer,
        children: [
            {
                path: 'profile',
                name: 'profile',
                component: Profile
            },
            {
                path: 'order',
                name: 'order',
                component: Order
            },
        ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

i'm using <router-view></router-view> in App.vue (main component). should i use it again?
Yes, you can use it within a parent component to render nested / children
0

If I understand correctly, you could try using dynamic component selection using the is attribute:

<template>
    <div class="row mb-3" id="customer-panel">
        <Breadcrumb></Breadcrumb>
        <CustomerSidebar></CustomerSidebar>
        <div class="col-lg-9">
            <component is="currentComponent"></component>
        </div>
    </div>
</template>

And associate currentComponent variable to the name of the component you want to use in the current route.

{
    path: '/customer',
    component: Profile,
    redirect: '/customer/profile',
    children: [
        {
            path: 'profile',
            name: 'profile',
            components: { default: Profile, currentComponent: MyComponent1 }
        },
        {
            path: 'order',
            name: 'order',
            component: {default: Order, currentComponent: MyComponent2 } 
        },
    ]
}

See:

1 Comment

this is not a direct vue-router implementation as asked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.