1.router-link和router-view組件
src/App.vie文件內容:
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
在App.vue文件代碼里有2個router-link組件和1個router-view組件
router-link組件實際上是封裝了一個a標簽,里面有一個重要的to屬性,to屬性指定的值是一個路徑
router-link標簽中有內容渲染,所以router-link標簽有開標簽和閉標簽
router-view組件是視圖渲染組件,通過router-link的to屬性加載的組件都會在router-view里被渲染
router-view標簽中沒有內容渲染,所以router-view標簽可以寫成 <router-view/> 單標簽的形式,這與 <router-view></router-view> 效果相同
router-link的to屬性指定的路徑就是src/router/router.js
文件中定義的路由
import Home from '@/views/Home.vue' // 引入Home.vue文件並設置引入名稱為Home,@標簽是在vue.config.js文件中定義的src路徑的別名
export default [ // 路由列表是一個數組,在這個數組中定義了路由對象
{ // 一個基本的路由對象包含path和component這兩個屬性
path: '/', // path屬性是在瀏覽器中輸入的跳轉路徑
component: Home, // component屬性是指定要加載渲染的組件名稱
}, {
path: '/about',
// 這里定義的是懶加載路由,即只有當訪問這個路由時,才會加載 src/views/About.vue這個組件
component: () => import('@/views/About.vue'),
}
]
2.路由配置
2.1 動態路由
在src/router/router.js
中新建一個路由對象
import Home from '@/views/Home.vue'
export default [
{
path: '/',
component: Home,
}, {
path: '/about',
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name', // 動態路由
component:() => import('@/views/argu.vue')
}
]
在src/views
目錄中創建argu.vue
文件
argu.vue
文件內容如下
<template>
<div>
{{ $route.params.name }}
</div>
</template>
<script>
export default {
}
</script>
在瀏覽器中輸入URL: http://localhost:8080/#/argu/apple
,頁面上會渲染URL中對應的參數
同樣的,當URL改成:http://localhost:8080/#/argu/orange
時,頁面上渲染的結果也相應改變了
在上面的例子里,$route代表的是當前加載頁面的路由對象
,$route.params代表當前加載頁面中的參數對象
所以$route.params.name
表示的就是當前加載頁面的參數對象中的name對應位置上的的值
不管瀏覽器的URL中輸入的是name值是什么,$route.params.name
都會被匹配到
這樣就可以起到組件復用,只需要傳遞不同的參數,同一個頁面就可以呈現不同的結果
2.2 嵌套路由
在實際開發中,經常會用到多層嵌套的組件,多層嵌套組件可以通過嵌套路由來完成
修改src/router/router.js
文件,新建嵌套路由
import Home from '@/views/Home.vue'
export default [
{
path: '/',
component: Home,
}, {
path: '/about',
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name',
component:() => import('@/views/argu.vue')
},{
path:'/parent', // 嵌套路由
component:() => import('@/views/parent.vue'),
children:[
{
path:'child',
component:() => import('@/views/child.vue')
}
]
}
]
然后在src/views
目錄下新建parent.vue
文件和child.vue
文件
parent.vue
文件內容如下
<template>
<div>
I am parent page
<router-view/>
</div>
</template>
<script>
export default {
}
</script>
child.vue
文件內容如下
<template>
<div>
I am child page
</div>
</template>
<script>
export default {
}
</script>
瀏覽器打開URL:http://localhost:8080/#/parent/child
,渲染結果如下
2.3 命名路由
修改src/router/router.js
文件,為路由命名
import Home from '@/views/Home.vue'
export default [
{
path: '/',
name: 'home', // 命名路由
component: Home,
}, {
path: '/about',
name: 'about', // 命名路由
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name',
component:() => import('@/views/argu.vue')
},{
path:'/parent',
component:() => import('@/views/parent.vue'),
children:[
{
path:'child',
component:() => import('@/views/child.vue')
}
]
}
]
修改App.vue文件,在router-link標簽中使用name來綁定路由名
<template>
<div id="app">
<div id="nav">
<router-link v-bind:to="{ name:'home'}">Home</router-link> |
<router-link :to="{ name:'about'}">About</router-link>
</div>
<router-view/>
</div>
</template>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>
v-bind:to 方法可以簡寫成: :to
使用瀏覽器訪問URL:http://localhost:8080/#/
,在頁面上點擊Home和About標簽,可以正常的渲染
2.4 命名視圖
如果想在同一個頁面上顯示多個視圖,並分別對不同的視圖進行布局時,可以在div標簽中定義多個router-view標簽,並為每個router-view標簽定義名字,這就是命名視圖
修改src/App.vue
文件,添加兩個命名視圖view1和view2
<template>
<div id="app">
<div id="nav">
<router-link v-bind:to="{ name:'home'}">Home</router-link> |
<router-link :to="{ name:'about'}">About</router-link>
</div>
<router-view/>
<router-view name="view1"/> // 添加router-view標簽,並把名字定義為view1
<router-view name="view2"/> // 添加router-view標簽,並把名字定義為view2
</div>
</template>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>
然后修改src/router/router.js
文件,在新添加的路由中添加兩個命名視圖
import Home from '@/views/Home.vue'
export default [
{
path: '/',
name: 'home',
component: Home,
}, {
path: '/about',
name: 'about',
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name',
component:() => import('@/views/argu.vue')
},{
path:'/parent',
component:() => import('@/views/parent.vue'),
children:[
{
path:'child',
component:() => import('@/views/child.vue')
}
]
},{
path:'/named_view', // 命名視圖
components:{
// default會默認加載App.vue中沒有命名的視圖
default: () => import('@/views/child.vue'),
view1: () => import('@/views/view1.vue'),
view2: () => import('@/views/view2.vue'),
}
}
]
然后在src/views
目錄下添加view1.vue和view2.vue文件
view1.vue文件內容為
<template>
<div>
I am view1
</div>
</template>
view2.vue文件內容為
<template>
<div>
I am view2
</div>
</template>
在瀏覽器中打開URL;http://localhost:8080/#/named_view
在瀏覽器中安裝 Vue Devtool插件,可以看到頁面上渲染了3個router-view組件
2.5 重定向路由
重定向路由的作用是把當前要訪問的URL重定向到另一個URL
修改src/router/router.js
文件,添加重定向路由
import Home from '@/views/Home.vue'
export default [
{
path: '/',
name: 'home',
component: Home,
}, {
path: '/about',
name: 'about',
// route level code-splitting
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name',
component:() => import('@/views/argu.vue')
},{
path:'/parent',
component:() => import('@/views/parent.vue'),
children:[
{
path:'child',
component:() => import('@/views/child.vue')
}
]
},{
path:'/about_path', // 重定向路由
redirect:'about'
}
]
當使用瀏覽器訪問URL:http://localhost:8080/#/about_path
時,就會跳轉到路由名為 about的路由上去,即訪問了/about
路由
在重定向路由中,也可以使用命名路由的方式
即把/about_path修改成使用命名路由的重定向方式
{
path: '/about_path',
redirect: {
name:'about'
}
}
在重定向的redirect中,也可以傳入一個函數
修改about_path路由
{
path: '/about_path',
redirect: to => {
console.log(to) // 打印出傳入的to函數
}
}
瀏覽器打開URL:http://localhost:8080/#/about_path
,在調試樣中查看打印出的結果
修改about_path路由,在redirect中定義函數體
{
path: '/about_path',
redirect: to => {
return{
name:'home'
}
}
}
刷新瀏覽器,頁面就跳轉到home頁面了
redirect中定義的to函數也可以返回一個路由路徑
{
path: '/about_path',
redirect: to => {
return '/named_view'
}
}
此時打開URL:http://localhost:8080/#/about_path
,頁面就會跳轉到/named_view
頁面中
上面redirect中的函數體可以簡寫成
{
path: '/about_path',
redirect: to => '/named_view'
}
2.6 路由別名
路由別名,顧名思義就是為一個路由設置一個別名,設置別名以后即可以通過路由的path的值進行訪問,也可以通過別名訪問這個路由
修改src/router/router.js
文件,為名為"home"的路由設置別名
import Home from '@/views/Home.vue'
export default [
{
path: '/',
name: 'home',
alias:'/home_page',
component: Home,
}, {
path: '/about',
name: 'about',
component: () => import('@/views/About.vue'),
},{
path:'/argu/:name',
component:() => import('@/views/argu.vue')
},{
path:'/parent',
component:() => import('@/views/parent.vue'),
children:[
{
path:'child',
component:() => import('@/views/child.vue')
}
]
},{
path:'/named_view',
components:{
default: () => import('@/views/child.vue'),
view1: () => import('@/views/view1.vue'),
view2: () => import('@/views/view2.vue'),
}
}, {
path: '/about_path',
redirect: to => '/named_view'
}
]
這樣不管訪問URL:http://localhost:8080/#/
,還是URL:http://localhost:8080/#/home_page
,都可以訪問到指定的視圖了
2.7 JS操作路由
JS操作路由就是通過JS控制頁面的跳轉和返回
首先修改src/views/Home.vue
文件,在頁面上添加一個button按鈕
,button按鈕的值為"返回上一頁"
,button點擊事件為"handleClick"
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<button @click="handleClick">返回上一頁</button> // 新添加的button按鈕
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'home',
components: {
HelloWorld
},
methods:{
handleClick () {
this.$router.back()
}
}
}
</script>
瀏覽器打開URL:http://localhost:8080/#/
,點擊About標簽跳轉到about頁面
然后再點擊Home標簽,跳轉到home頁面
點擊"返回上一頁"按鈕,頁面返回到當前頁的上一頁,即about頁面了
返回上一頁的JS語句也可以寫成:
handleClick () {
this.$router.go(-1)
}
或者跳轉到指定頁面:
handleClick () {
this.$router.push({
name:'about',
})
}
跳轉到指定頁面的同時需要在URL上添加參數時,可以加query選項
handleClick () {
this.$router.push({
name:'about',
query:{
name:'orange',
price:'5'
}
})
}
在home頁點擊"返回上一頁"時,URL會跳轉到about頁面,並在URL上添加query中定義的參數
跳轉到前面定義的/argu
這個路由
先在src/router/router.js
文件中,為/argu這個路由添加名字
{
path:'/argu/:name',
name:'argu',
component:() => import('@/views/argu.vue')
}
然后修改src/views/Home.vue
文件中的handleClick方法
handleClick () {
this.$router.push({
name:'argu',
params:{
name:'orange',
}
})
}
在home頁點擊"返回上一頁"時,URL會跳轉到about頁面
上面的方法也可以用ES6的語法重寫,也可以達到同樣的效果
handleClick () {
const name = 'banana'
this.$router.push({
path:`/argu/${name}` // 這里的 ` 是鍵盤上Ese下面的那個鍵
})
}
或者
handleClick () {
this.$router.push({
name:`argu`,
params:{
name:'banana'
}
})
}
在home頁點擊"返回上一頁"時,瀏覽器頁面顯示為