vue-router學習html
const User = { template: '...', watch: { '$route' (to, from) { // 對路由變化做出響應... } } }
- 字符串 router.push('home') - 對象 router.push({ path: 'home' }) - 命名的路由 router.push({ name: 'user', params: { userId: 123 }}) - 帶查詢參數,變成 /register?plan=private router.push({ path: 'register', query: { plan: 'private' }})
const router = new VueRouter({ routes: [ { path: '/user/:userId', name: 'user', component: User } ] })
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
router.push({ name: 'user', params: { userId: 123 }})
<router-view class="view one"></router-view> <router-view class="view two" name="a"></router-view> <router-view class="view three" name="b"></router-view>
const router = new VueRouter({ routes: [ { path: '/', components: { default: Foo, a: Bar, b: Baz } } ] })
const router = new VueRouter({ routes: [ { path: '/a', redirect: '/b' }, { path: '/c', redirect: { name: 'foo' }}, { path: '/d', redirect: to => { // 方法接收 目標路由 做爲參數 // return 重定向的 字符串路徑/路徑對象 }} ] })
const router = new VueRouter({ routes: [ { path: '/a', component: A, alias: '/b' } ] })