Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  92] [ 7]  / answers: 1 / hits: 5474  / 2 Years ago, mon, april 11, 2022, 12:00:00

I am working on a vue 2 app using vue 3 router. I want to have a route like /user and within that /user/profile and /user/settings. So, I configured my vue router.js file as


router.js


import Vue from "vue";
import Router from "vue-router";
import User from "../components/User";
import Settings from "../components/Settings";
import Profile from "../components/Profile";

Vue.use(Router);

export default new Router({
mode: "history",
routes: [
{
path: "/user",
name: "user",
component: User,
children: [
{
path: "/settings",
name: "settings",
component: Settings
},
{
path: "/profile",
name: "profile",
component: Profile
}
]
}
]
});

User.vue


<template>
<h1>User</h1>
</template>

Settings.vue


<template>
<h1>Settings</h1>
</template>

Profile.vue


<template>
<h1>Settings</h1>
</template>

App.vue


<template>
<div id="app">
<router-view></router-view>
</div>
</template>

<script>
export default {
name: "app",
};
</script>

<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

Whenever I am visiting /user, the User.vue is rendered but when I am trying to visit /user/profile or /user/settings, nothing is rendered in the DOM. I followed Child route component not rendering in vue js and Activate router-link that has multi level nested routes with CRUD setup but no luck. What I want is User.vue should be rendered on /user route and only the contents of Profile.vue should be rendered when we are in /user/profile route. I tried to include <router-view /> in User.vue but I am getting both User.vue and Profile.vue contents in /user/profile. I am attaching codesandbox link to see it in live and to get a better understanding as I am not sure how to set it up in SO.


Link: https://codesandbox.io/s/nostalgic-sky-3wvhqr?file=/App.vue:0-354


How can I achieve this?


More From » vue.js

 Answers
7

UserParent.vue


<template>
<router-view />
</template>

router.js


routes: [
{
path: "/user",
component: UserParent,
children: [
{
path: "/",
name: "user",
component: User
},
{
path: "/settings",
name: "settings",
component: Settings
},
{
path: "/profile",
name: "profile",
component: Profile
}
]
}
]

[#204] Thursday, March 31, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;