猿问

Vue 2.0 页面刚启动时如何做路由的重定向?

如果不做重定向,页面就会空,比如刚打开时 url 是 localhost:8080,我希望能重定向到 Index 组件,即 localhost:8080/index,现在我是给 index 组件加了个别名 alias '/' 解决,是否可以用程序方式解决?

因为我看之前 vue 1 别人的代码有这样写:

router.redirect({  '*': '/index'})

现在 vue 2 是不行了吗?

补充: 看了楼下的回复,本来觉得 replace 不错,但是 replace 有个问题,如果打开的是别的页面,也会被 replace 过去 ...


杨__羊羊
浏览 980回答 1
1回答

动漫人物

//routesvar router = new VueRouter({    mode: 'hash',    routes: [         { path: '/index', component: require('./pages/index.vue') },         { path: "*", redirect: '/index' },     ] });这样重定向
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答