33

I want to default to a specific page when user navigates to the root path ie when used goes to myapp.com I want to redirect them to myapp.com/defaultpage

My current code is

index.js

import Full from '../containers/Full'
import DefaultView from '../views/DefaultView'

export default new Router({
  mode: 'history',
  linkActiveClass: 'open active',
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    {
      path: '/',
      redirect: '/defaultview',
      name: 'home',
      component: Full,
      children: [
        {
          path: '/defaultview',
          name: 'defaultview',
          component: DefaultView
        },
        {
          path: '*',
          component: NotFoundComponent
        }
    }
]})

As it is when user goes to myapp.com I get a '404 page not found' - ie the NotFoundComponent. Only when I type in myapp.com/defaultview can I get to the correct page.

Any ideas?

2
  • remove / in children Commented May 19, 2017 at 2:59
  • I tried, still same 404, I also tried removing the / from root ie redirect: 'defaultview'. Also 404. Only way I can get to my page is via myapp.com/defaultview Commented May 19, 2017 at 3:50

4 Answers 4

38

Try this code:

routes: [
    {
      path: '/',
      redirect: '/defaultview'
    },
    {
      path: '/defaultview',
      name: 'defaultview',
      component: DefaultView
    },
    {
      path: '*',
      component: NotFoundComponent
    }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, however I need to keep the Full component for the root path as it is the base for all other components to sit in. If I add that back then basically is the same as what I have above.
One hack that works but wont work for more than one page where I need to redirect to is: { path: '*', component: DefaultView }
Very useful information. Thank you for sharing. I wonder if you experienced Nuxt.js and did the same thing with it? In the past I asked this same question but with Nuxt.js, when no one answered me for several weeks, I deleted my question.
@BillalBegueradj With Nuxt.js you need module as router-module (github.com/nuxt-community/router-module)
16

When using children remove url prefix of the parent

ex: change "/defaultview" to defaultview remove the parent path component, so the actual code should be like this

routes: [
    {
      path: '/',
      redirect: '/defaultview',
      name: 'home',
      component: Full,
      children: [
        {
          path: 'defaultview', /* changed */
          name: 'defaultview',
          component: DefaultView
        },
        {
          path: '*',
          component: NotFoundComponent
        }
      ]
    }
];

reference Nested Routes

Comments

2

This way is works for me

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Home from '../components/home/container';
import LiveAgent from '../components/live_agent/container';
import Bot from '../components/bot/container';
import User from '../components/user/container';

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        component: Home,
        name: 'home',
        path: '/home'
    },
    {
        component: LiveAgent,
        name: 'live_agent',
        path: '/live_agent'
    },
    {
        component: Bot,
        name: 'bot',
        path: '/bot'
    },
    {
        component: User,
        name: 'user',
        path: '/user'
    }
];

export default new VueRouter({
    routes // short for routes: routes
})

Comments

1

You can do it using 1 line of code, i.e. by adding router.replace("myPath");. Full code:

import Vue from "vue";
import Router from "vue-router";
import MyComponent from "./my-component";

Vue.use(Router);

const routes = [
  { path: "/myPath", name: "myPath", component: MyComponent }
];

const router = new Router({
  mode: "history", // Remove the hash from the URL, optional.
  routes
});

router.replace("myPath");

export default router;

Comments

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.