Vue學(xué)習(xí)筆記[13]-使用vue-router

在使用vue cli創(chuàng)建項(xiàng)目時(shí)可以添加vue-router,或使用npm安裝vue-router

配置路由跳轉(zhuǎn):

// ./src/router/index.js
//1. 導(dǎo)入
import Vue from 'vue'
import VueRouter from 'vue-router'
//導(dǎo)入組件
import Home from '../components/Home'
import About from '../components/About'
//安裝Vue-router
Vue.use(VueRouter)
//在路由對(duì)象中配置路由
const routes = [
  {
    path:'/home',
    component: Home
  },

  {
    path:'/about',
    component:About
  }
]
//全局路由控制器
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//4. 導(dǎo)出全局路由控制器并使用
export default router
//./src/main.js
// 導(dǎo)入全局控制器
import router form './router'
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

//導(dǎo)入文件夾會(huì)自動(dòng)查找index.js

實(shí)踐使用:

1. 創(chuàng)建路由組件:

components文件夾下創(chuàng)建組件并導(dǎo)出

<template>
  <div>
      <h2>home</h2>
      <p>Home_text</p>
  </div>
</template>

<script>
export default {
    name:'Home'
};
</script>

<style scoped>
</style>
<template>
  <div>
    <h2>About</h2>
    <p>About_text</p>
  </div>
</template>

<script>
  export default {
    name:'About'
  }
</script>

<style scoped>

</style>
2. 配置路由映射

router文件夾下的index.js導(dǎo)入組件并配置路的關(guān)系

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home'
import About from '../components/About'
Vue.use(VueRouter)

const routes = [
  {
    path:'/home',
    component: Home
  },

  {
    path:'/about',
    component:About
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

3. 在所需位置通過<router-link><router-view>使用路由

//app.vue
<template>
  <div id="app">
    <router-link to='/home'> HOME </router-link>    
    <router-link to='/about'> ABOUT </router-link>    
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  
}
</script>

<style>

</style>

<router-link>和<router-view>

<router-link>

<router-link>激活時(shí)的class:

當(dāng)<router-link>處于激活狀態(tài)時(shí),會(huì)對(duì)渲染出來的替換<router-link>的標(biāo)簽添加class=“router-link-exact-active router-link-active”屬性,可在<style>標(biāo)簽中添加樣式。

默認(rèn)添加的class

改變默認(rèn)的class名稱,可用active-class屬性。
<router-link active-class='active' to='/about'> ABOUT </router-link>
或者在全局路由配置中復(fù)寫linkActiveClass
const router = new VueRouter({...,linkActiveClass:'active'})


<router-link>改變history為replace模式:

<router-link replace to='/about'> ABOUT </router-link>    

替換<router-link>的形式:

<router-link>是vue-router中的已經(jīng)內(nèi)置的全局組件,<router-link>默認(rèn)渲染為<a>標(biāo)簽,如果想替換成其他的標(biāo)簽,可是用以下方法進(jìn)行替換:

1. 使用原生的標(biāo)簽進(jìn)行替換:

Vue的所有的組件中都繼承了Vue原型組件的$router屬性,因此可以在事件監(jiān)聽中修改$router,通過在 Vue 根實(shí)例的 router 配置傳入 router 實(shí)例,下面這些屬性成員會(huì)被注入到每個(gè)子組件。

  • this.$router

    router 實(shí)例。

  • this.$route

    當(dāng)前激活的路由信息對(duì)象。這個(gè)屬性是只讀的,里面的屬性是 immutable (不可變) 的,不過你可以 watch (監(jiān)測變化) 它。

<template>
  <div id="app">
    <!-- <router-link to='/about' tag='button'> ABOUT </router-link>    
    <router-link to='/home' tag='button'> HOME </router-link>-->
    <button @click.prevent="homeBtnClick">HOME</button>
    <button @click.prevent="aboutBtnClick">ABOUT</button>
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  name: "App",
  methods: {
    homeBtnClick() {
      if(this.$router.currentRoute.path !== '/home')//vue-router 不允許多次push同一個(gè)url
//if(this.$route.path!=='/about')
      this.$router.replace("/home");
    },
    aboutBtnClick() {
      if(this.$router.currentRoute.path !== '/about')
      this.$router.replace("/about");
    }
  }
}
</script>

錯(cuò)誤信息

vue-router不允許多次push同一個(gè)url,因此要檢測上一次的url是否為當(dāng)前url
if(this.$route.path!=='/about')也可以

2. 使用tag屬性替換默認(rèn)形式
<router-link tag='button' to='/about'> ABOUT </router-link>    

<router-view>

<router-view>會(huì)根據(jù)當(dāng)前的路徑動(dòng)態(tài)地渲染出不同的組件。網(wǎng)頁中的其他內(nèi)容,比如頂部的標(biāo)題、導(dǎo)航等會(huì)和<router-view>處于同一層級(jí)。
在處理路由切換時(shí),切換的是<router-view>掛載的組件,其他的內(nèi)容不會(huì)發(fā)生改變。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 路由,其實(shí)就是指向的意思,當(dāng)我點(diǎn)擊頁面上的home按鈕時(shí),頁面中就要顯示home的內(nèi)容,如果點(diǎn)擊頁面上的about...
    六月太陽花閱讀 698評(píng)論 0 3
  • SPA單頁應(yīng)用 傳統(tǒng)的項(xiàng)目大多使用多頁面結(jié)構(gòu),需要切換內(nèi)容的時(shí)候我們往往會(huì)進(jìn)行單個(gè)html文件的跳轉(zhuǎn),這個(gè)時(shí)候受網(wǎng)...
    視覺派Pie閱讀 12,180評(píng)論 1 55
  • vue-router 基本使用Vue Router 官方網(wǎng)介紹 路由,其實(shí)就是指向的意思,當(dāng)我點(diǎn)擊頁面上的home...
    雄關(guān)漫道從頭越閱讀 2,242評(píng)論 0 0
  • 1路由,其實(shí)就是指向的意思,當(dāng)我點(diǎn)擊頁面上的home按鈕時(shí),頁面中就要顯示home的內(nèi)容,如果點(diǎn)擊頁面上的abou...
    你好陌生人丶閱讀 1,774評(píng)論 0 6
  • [轉(zhuǎn)載自](http://www.cnblogs.com/SamWeb/p/6610733.html) 路由,其實(shí)...
    可爸閱讀 1,648評(píng)論 1 0

友情鏈接更多精彩內(nèi)容