编程式导航,指的是通过方法设置导航。

    router.push()用来导航到一个新URL

    编程式导航 - 图1

    的to属性使用绑定方式时也可是属性对象结构。

    编程式导航 - 图2

    <!DOCTYPE html> <html lang=“en”> <head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <title>Document</title> </head> <body> <div id=“app”> <router-link :to=“{ path: ‘/user/700’ }”>用户700</router-link> <router-view></router-view> </div> <script src=“./lib/vue.js”></script> <script src=“./lib/vue-router.js”></script> <script> var User = { template: <div> 这是用户 {{ $route.params.id }} </div> }; var routes = [ { path: ‘/user/:id’, component: User } ]; var router = new VueRouter({ routes }); var vm = new Vue({ el: ‘#app’, router }); </script> </body> </html>