代碼示例
<div id="app">
<p>$route.name: {{$route.name}}</p>
<p>$route.path: {{$route.path}}</p>
<p>$route.query: {{$route.query}}</p>
</div>
<script src="/static/js/vue.js"></script>
<script src="/static/js/vue-router.js"></script>
<script>
const router = new VueRouter({
mode: "history", // 默認使用hash模式,url會出現#
});
const app = new Vue({
router,
el: "#app",
});
</script>
顯示結果
$route.name:
$route.path: /demo.html
$route.query: { "id": "44", "name": "Tom" }
單頁面上使用vue-router,可以用來解析url上的參數