1. 路由
1.1 vue-router動態路由匹配
1.1.1 動態匹配路由的基本用法
應用場景:通過動態路由引數的模式進行路由匹配,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
//路由組件中通過$route.params獲取路由引數
template: '<h1>User 組件 -- 用戶id為: {{$route.params.id}}</h1>'
}
const Register = {
template: '<h1>Register 組件</h1>'
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user'},
//動態路徑引數 以冒號開頭
{ path: '/user/:id', component: User },
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

1.1.2 路由組件傳遞引數
$route與對應路由形成高度耦合,不夠靈活,所以可以使用props將組件和路由解耦,
(1) props的值為布爾型別
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id'], //使用 props 接收路由引數
template: '<h1>User 組件 -- 用戶id為: {{id}}</h1>' //使用路由引數
}
const Register = {
template: '<h1>Register 組件</h1>'
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user'},
//如果 props 被設定為 true,route.params 將會被設定為組件屬性
{ path: '/user/:id', component: User, props: true },
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

(2) props的值為物件型別
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id', 'uname', 'age'],
template: '<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>'
}
const Register = {
template: '<h1>Register 組件</h1>'
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user'},
//如果 props 是一個物件,它會被按原樣設定為組件屬性
{ path: '/user/:id', component: User, props: { uname: 'lisi', age: 20 } },
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

(3) props的值為函式型別
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link to="/user/3">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id', 'uname', 'age'],
template: '<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>'
}
const Register = {
template: '<h1>Register 組件</h1>'
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user' },
//如果 props 是一個函式,則這個函式接收 route 物件為自己的形參
{
path: '/user/:id',
component: User,
props: route => ({ uname: 'zs', age: 20, id: route.params.id })
},
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

1.2 vue-router命名路由
為了更加方便的表示路由的路徑,可以給路由規則起一個別名,即為“命名路由”,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id', 'uname', 'age'],
template: '<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>'
}
const Register = {
template: '<h1>Register 組件</h1>'
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user' },
{
// 命名路由
name: 'user',
path: '/user/:id',
component: User,
props: route => ({ uname: 'zs', age: 20, id: route.params.id })
},
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

1.3 vue-router編程式導航
頁面導航的兩種方式:
宣告式導航:通過點擊鏈接實作導航的方式,叫做宣告式導航
例如:普通網頁中的<a></a>鏈接或vue中的鏈接<router-link></router-link>
編程式導航:通過呼叫JavaScript形式的API實作導航的方式,叫做編程式導航
例如:普通網頁中的 location.href
1.3.1 編程式導航基本用法
常用的編程式導航 API 如下:
this.$router.push('hash地址')
this.$router.go(n)

1.3.2 編程式導航引數規則
router.push() 方法的引數規則:
字串(路徑名稱)
router.push('/home')
物件 router.push({ path: '/home' })
命名的路由(傳遞引數) router.push({ name: '/user', params: { userId: 123 }})
帶查詢引數,變成 /register?uname=lisi
router.push({ path: '/register', query: { uname: 'lisi' }})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!-- 匯入 vue 檔案 -->
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue_2.5.22.js"></script>
<script src="https://www.cnblogs.com/liuhui0308/p/lib/vue-router_3.0.2.js"></script>
</head>
<body>
<!-- 被 vm 實體所控制的區域 -->
<div id="app">
<router-link to="/user/1">User1</router-link>
<router-link to="/user/2">User2</router-link>
<router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
<router-link to="/register">Register</router-link>
<!-- 路由占位符 -->
<router-view></router-view>
</div>
<script>
const User = {
props: ['id', 'uname', 'age'],
template: `<div>
<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>
<button @click="goRegister">跳轉到注冊頁面</button>
</div>`,
methods: {
goRegister() {
this.$router.push('/register')
}
},
}
const Register = {
template: `<div>
<h1>Register 組件</h1>
<button @click="goBack">后退</button>
</div>`,
methods: {
goBack() {
this.$router.go(-1)
}
}
}
// 創建路由實體物件
const router = new VueRouter({
// 所有的路由規則
routes: [
{ path: '/', redirect: '/user' },
{
// 命名路由
name: 'user',
path: '/user/:id',
component: User,
props: route => ({ uname: 'zs', age: 20, id: route.params.id })
},
{ path: '/register', component: Register }
]
})
// 創建 vm 實體物件
const vm = new Vue({
// 指定控制的區域
el: '#app',
data: {},
// 掛載路由實體物件
// router: router
router
})
</script>
</body>
</html>
執行效果:

點擊按鈕,跳轉到注冊界面,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/939.html
標籤:JavaScript
