首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >vue-路由器重定向至默认路径问题

vue-路由器重定向至默认路径问题
EN

Stack Overflow用户
提问于 2017-05-19 10:21:08
回答 4查看 38.3K关注 0票数 27

当用户导航到根路径时,我想默认到一个特定的页面,即当使用时转到myapp.com,我想将它们重定向到myapp.com/defaultpage

我当前的代码是

index.js

代码语言:javascript
运行
复制
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
        }
    }
]})

因为它是当用户去myapp.com时,我得到一个'404页未找到‘-即NotFoundComponent。只有当我输入myapp.com/defaultview时,我才能进入正确的页面。

有什么想法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-02 14:40:53

使用子项时,删除父项的url前缀。

例如:将"/defaultview"更改为defaultview删除父路径组件,因此实际代码应如下所示

代码语言:javascript
运行
复制
routes: [
{
  path: '/',
  redirect: '/defaultview',
  name: 'home',
  component: Full,
  children: [
    {
      path: 'defaultview', /* changed */
      name: 'defaultview',
      component: DefaultView
    },
    {
      path: '*',
      component: NotFoundComponent
    }
  ]
}

参考Nested Routes

票数 12
EN

Stack Overflow用户

发布于 2017-05-19 11:02:36

尝试以下代码:

代码语言:javascript
运行
复制
routes: [
    {
      path: '/',
      redirect: '/defaultview'
    },
    {
      path: '/defaultview',
      name: 'defaultview',
      component: DefaultView
    },
    {
      path: '*',
      component: NotFoundComponent
    }
]
票数 33
EN

Stack Overflow用户

发布于 2020-03-19 10:47:14

这种方式对我很有效

代码语言:javascript
运行
复制
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
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44060414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档