1. 程式人生 > >react router @4 和 vue路由 詳解(一)vue路由基礎和使用

react router @4 和 vue路由 詳解(一)vue路由基礎和使用

完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html

1、vue路由基礎和使用

  a、大概目錄

           我這裡建了一個router資料夾,資料夾下有index.html

 


 

 

  b、準備工作:

    npm install vue-router

    或者 yarn add vue-router

 


 

 

  c、配置

    必須要通過 Vue.use() 明確地安裝路由功能:
    在你的資料夾下的 src 資料夾下的 main.js 檔案內寫入以下程式碼

import Vue from 'vue'

import VueRouter from 'vue-router'

Vue.use(VueRouter)

    附上我的程式碼:我是將router的內容寫在了我的router資料夾下的index.html中,然後暴露出去,在main.js中引入

    router資料夾下的index.html

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import Home from 'pages/Home'
import Map from 'components/Map'
import Home1 from 'components/Home1'
import Find from 'components/Find'
import Mine from 'components/Mine'
import Type from 'components/Type'
import Publish from 'components/Publish'
import Search from 'components/Search'
import Success from 'components/Success'
import Need from 'components/Need'
import Position0 from 'components/Position'
import Like from 'components/scrollX/Like'
import S1 from 'components/scrollX/1'
import S2 from 'components/scrollX/2'
import Listall from 'components/mine/Listall'
import Listone from 'components/mine/Listone'
import Listchange from 'components/mine/Listchange'

const routes = [
    {
        path:'/',
        redirect:'/ho'
    },
    {
        path: '/ho',
        redirect:'/ho/home',
        component: Home,
        children: [
            {
                name: 'home',
                path: 'home',
                component: Home1,
                redirect:'/ho/home/like',
                children :[
                    {
                        name: 'like',
                        path: 'like',
                        component: Like
                    },
                    {
                        name: '2000001',
                        path: '2000001',
                        component: S1
                    },
                    {
                        name: '2000022',
                        path: '2000022',
                        component: S2
                    }
                ]
            },
            {
                name: 'type',
                path: 'type',
                component: Type
            },
            {
                name: 'need',
                path: 'need',
                component: Need
            },
            {
                name: 'find',
                path: 'find',
                component: Find
            },
            {
                name: 'mine',
                path: 'mine',
                component: Mine
            }
        ]
    },
    {
        name: 'search',
        path: '/search',
        component: Search
    },
    {
        name: 'position',
        path: '/position',
        component: Position0
    },
    {
        name: 'publish',
        path: '/publish',
        component: Publish
    },
    {
        name: 'success',
        path: '/success',
        component: Success
    },
    {
        name: 'listall',
        path: '/listall',
        component: Listall
    },
    {
        name: 'listone',
        path: '/listone',
        component: Listone
    },
    {
        name: 'listchange',
        path: '/listchange',
        component: Listchange
    },
    {
        name: 'map',
        path: '/map',
        component: Map
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

export default router
複製程式碼

    main.js

複製程式碼
import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.use(MintUI)
Vue.use(ElementUI);

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
複製程式碼

 


 

  d、常規使用

     <router-view></router-view>
路由匹配到的元件將渲染在這裡     你可以把他理解為一個版塊,比如現在有一個home頁面,分為兩部分,內容部分和ibar部分,如圖:          這五個頁面共用下面的導航欄,只有導航欄上面的內容不同     <router-view></router-view>就可以寫在<Ibar></Ibar>的上面 複製程式碼
<template>
    <div class="home">
        <router-view></router-view>
        <Ibar></Ibar>
    </div>
</template>
複製程式碼

    那麼在Ibar頁面中如何切換路由呢?

複製程式碼
<template>
    <div class="ibar">
        <router-link to="/ho/home" tag="span" active-class="active">首頁</router-link>
        <router-link to="/ho/type" tag="span" active-class="active">類別</router-link>
        <router-link to="/ho/need" tag="span" active-class="active">需求</router-link>
        <router-link to="/ho/find" tag="span" active-class="active">發現</router-link>
        <router-link to="/ho/mine" tag="span" active-class="active">我的</router-link>
    </div>
</template>
複製程式碼

    注意:此處的tag=“span”代表這個按鈕是個span標籤,你可以寫樣式的時候直接寫span標籤的樣式即可

       此處的active-class="active"代表點選哪個按鈕哪個按鈕高亮

    

    此時我們詳細看一下router資料夾下的index.js

複製程式碼
//引入vue   

import Vue from 'vue'

//引入路由
import VueRouter from 'vue-router'

//把路由掛載到vue上
Vue.use(VueRouter)
//引入我各個路由對應的component元件
import Home from 'pages/Home' import Map from 'components/Map' import Home1 from 'components/Home1' import Find from 'components/Find' import Mine from 'components/Mine' import Type from 'components/Type' import Publish from 'components/Publish' import Search from 'components/Search' import Success from 'components/Success' import Need from 'components/Need' import Position0 from 'components/Position' import Like from 'components/scrollX/Like' import S1 from 'components/scrollX/1' import S2 from 'components/scrollX/2' import Listall from 'components/mine/Listall' import Listone from 'components/mine/Listone' import Listchange from 'components/mine/Listchange'
const routes = [ {

     //path是路由的路徑
path:'/',

     //redirect代表重定向,因為當前路徑'/'並沒有對應的元件,所以需要重定向到其他路由頁面
     redirect:'/ho' }, { path: '/ho', redirect:'/ho/home',

     //當不需要重定向的時候,需要component寫上當前路由對應的元件頁面
component: Home,

     //有些路由還有子路由,需要用到children[],
     //當訪問的時候,<router-link>的屬性to的時候要把所有的父元件都帶上
     //如:此處的/ho/home/like
children: [ { name: 'home', path: 'home', component: Home1, redirect:'/ho/home/like', children :[ { name: 'like', path: 'like', component: Like }, { name: '2000001', path: '2000001', component: S1 }, { name: '2000022', path: '2000022', component: S2 } ] }, { name: 'type', path: 'type', component: Type }, { name: 'need', path: 'need', component: Need }, { name: 'find', path: 'find', component: Find }, { name: 'mine', path: 'mine', component: Mine } ] }, { name: 'search', path: '/search', component: Search }, { name: 'position', path: '/position', component: Position0 }, { name: 'publish', path: '/publish', component: Publish }, { name: 'success', path: '/success', component: Success }, { name: 'listall', path: '/listall', component: Listall }, { name: 'listone', path: '/listone', component: Listone }, { name: 'listchange', path: '/listchange', component: Listchange }, { name: 'map', path: '/map', component: Map } ] const router = new VueRouter({

//此處設定mode為history,即不帶#號,在處理資料的時候會更方便一些
    mode: 'history',

//ES6的寫法,即routes:routes的簡寫,當key和value名字一樣時,可簡寫
routes })
//把你建立的路由暴露出去,使得main.js可以將其引入並使用
export default router
複製程式碼

    引申1:

    路由有一個meta屬性

    可以給該路由掛載一些資訊

    設定一些自己title、顯示隱藏、左右滑動的方向之類的

meta: {
      title: "HelloWorld",    要現實的title
      show: true               設定導航隱藏顯示
}

    使用的時候:this.$route.meta.show

    <Bottom v-show="this.$route.meta.show"></Bottom>

 

    引申2:

    動態路由

{
    path:"/two/:id",
    component:Two,
}

    獲取資料this.$route.params.動態路由的名字

    此處是:this.$route.params.id

 

    引申3:

    路由別名alias

複製程式碼
{ 
    path: '/a', 
    component: A, 
    alias: '/b' 
}
//  /a 的別名是 /b
//意味著,當用戶訪問 /b 時,URL 會保持為 /b,但是路由匹配則為 /a
//就像使用者訪問 /a 一樣
//簡單的說就是給 /a 起了一個外號叫做 /b ,但是本質上還是 /a
複製程式碼