重定向:
在定義路由的時候,可以通過添加`redirect`參數,重定向到另外一個頁面。
routes: [{
path: "/",
redirect: "/article"
}]
別名:
在定義路由的時候,可以通過添加`alias`參數,表示該url的別名,以后也可以通過這個別名來訪問到這個組件。
let router = new VueRouter({
routes: [ {
path: "/article",
component: article,
alias: "/list"
}]
})
整體代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <title>VueRouter-路由重定向和別名</title> </head> <body> <div id="app"> <router-view></router-view> </div> <script> let article = Vue.extend({ template: "<h1>文章列表</h1>" }) let router = new VueRouter({ routes: [{ path: "/", redirect: "/article" }, { path: "/article", component: article, alias: "/list" }] }) new Vue({ el: "#app", router, }) </script> </body> </html>