最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解vue?route介紹、基本使用、嵌套路由

 更新時間:2022年08月24日 11:19:59   作者:小余努力搬磚  
vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構建單頁面應用,這篇文章主要介紹了vue?route介紹、基本使用、嵌套路由,需要的朋友可以參考下

前言

想要學習完整內(nèi)容請關注主頁的專欄————>Vue學習

本次的代碼段是結合體,被我分開發(fā)文,我以在看代碼段時,已經(jīng)截圖展示,所看部分

一、介紹、安裝

1.定義

vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構建單頁面應用。

路由:route 一組key-v的對應關系(路徑的改變對應的組件進行切換)

路由器:router 多個路由需要路由器管理

為了實現(xiàn)單頁面應用

2.安裝

npm i vue-router@3 安裝3版本

如果使用 vue ui 就沒有以下的操作,因為在創(chuàng)建項目的時候已經(jīng)配置好了

1:在src根目錄創(chuàng)建router目錄,在目錄中創(chuàng)建index.js,代碼如下:

import Vue from 'vue';
 
//導入vue-router
 
import VueRouter from 'vue-router'
 
//應用插件
 
Vue.use(VueRouter)
 
//創(chuàng)建router規(guī)則對象
 
const routes = [
 
]
 
//創(chuàng)建router
 
const router = new VueRouter({
 
routes
 
})
 
//導出router
 
export default router

2:main.js 中進行掛載

import Vue from 'vue'
import App from './App.vue'
 
import router from './router'
 
 
Vue.config.productionTip = false
 
new Vue({
 
  router,
 
  render: h => h(App)
}).$mount('#app')

二、基本使用(代碼后賦)

以下例子展現(xiàn)路由的基本使用

css樣式已經(jīng)寫好了,直接實現(xiàn)路由效果

展示效果

首先學習的效果

a743d40856164a5d919350fbf1bbc6e4.gif

53519f75c0f34dd892d50c2016be561d.png

a22d986709ed44fe81e2f90b021abcc2.png

d113c48804da4c0faa074fcb9d16edf4.png

2ca35bcf17f54ed785eb7b0a905b6f23.png

代碼(看對應的代碼段) app.vue代碼,此代碼含有樣式

<template>
  <div id="root">
			 <div class="main">
				  <div class="header">
					    <h1>路由的演示</h1>  
						<button @click="back">后退</button>
				  </div>
				
			  </div>
			  <div class="main">
				  <div class="left">
				  		<ul>
							<li><router-link to="/about" active-class="hover">公司簡介</router-link></li>
							<li><router-link to="/contaactus" active-class="hover">聯(lián)系方式</router-link></li>
              				<li><router-link to="/persons" active-class="hover">公司人員</router-link></li>
						</ul>	  
				  </div>
				  <div class="right">
					 <router-view></router-view>
				  </div>
				  <div style="clear: both;"></div>
			  </div>
			
		</div>
</template>
 
<script>
 
 
export default {
name:'App',
methods: {
 back(){
		this.$router.back()
	}
},
 
 
 
components:{
   
},
}
</script>
 
<style>
.c{
	clear: both;
}
*{
		margin: 0px;
		padding: 0px;
	}
	li{
		list-style: none;
	}
	a{text-decoration: none;}
	.main{width: 800px;margin: auto;}
	.header{box-sizing: border-box;padding: 20px;border:1px solid #666;}
	.left{
		height: 500px;
		border: 1px solid #666;
		width: 200px;
		float: left;
	}
	.left li{
		height: 50px;
		line-height: 50px;
		text-align: center;
		border-bottom: 1px solid #666;
		width: 100%;
	}
	.left li a{
		color: #333;display: block;
	}
	.left li a.hover{
		background: blue;color: #fff;
	}
 
	.right{float: right;
	border:1px solid #61DAFB;
	width: 590px;
	height: 500px;
	}
	.nav li{
		float: left;
	
	}
	.nav li a{
		width: 150px;
		text-align: center;
		height: 40px;line-height: 40px;
		text-align: center;
		border:1px solid #000000;
		display: block;
	}
	.nav li a.hover{
	background: #0000FF;color: #fff;
	}
</style>

三個路由組件的代碼

about

<template>
<div>
    <!-- <div class="left"> -->
    <ul class="nav">
	<li><router-link to="/about/year" active-class="hover">創(chuàng)建年份</router-link></li>
	<li><router-link to="/about/people" active-class="hover">創(chuàng)建人</router-link></li>
    </ul>  
    <!-- </div> -->
   <keep-alive include="People">
    <router-view class="c"></router-view>
   </keep-alive> 
	
 
</div>
</template>
 
<script>
export default {
    name: 'About',
 
    data() {
        return {
            
        };
    },
 
    mounted() {
        
    },
 
    methods: {
        
    },
};
</script>
 
<style scoped>
 
</style>

ContaactUs

<template>
    <div>
        聯(lián)系方式
    </div>
</template>
 
<script>
export default {
    name: 'ContaactUs',
 
    data() {
        return {
            
        };
    },
 
    mounted() {
        
    },
 
    methods: {
        
    },
};
</script>
 
<style scoped>
 
</style>

persons

<template>
  <div>
    <ul >
        <li v-for="item in persons" :key="item.id">
        <router-link :to="`/persons/show/${item.id}/${item.realname}`">姓名:{{item.realname}}</router-link>
        <!-- <router-link :to="`/persons/show/?id=${item.id}&realname=${item.realname}`">姓名:{{item.realname}}</router-link> -->
        <!-- <router-link :to="{name:'show',query:{id:item.id,realname:item.realname}}">姓名:{{item.realname}}</router-link> -->
        <button @click="push(item)">點擊跳轉</button>
        </li>
        
	</ul>
 
<hr>
    <router-view></router-view>	
  </div>
</template>
 
<script>
export default {
name:'Persons',
data(){
    return{
        persons:[
            {id:1,realname:'張三'},
            {id:2,realname:'李四'},
            {id:3,realname:'王五'},
            {id:4,realname:'趙六'}
        ]
    }
},
methods: {
  push(item){
    this.$router.push(`/persons/show/${item.id}/${item.realname}`)
  },
 
},
}
</script>
 
<style>
 
</style>

router

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import About from '../pages/About'
import ContaactUs from '../pages/ContaactUs'
import Persons from '../pages/Persons'
// import Show from '../pages/Show'
// import Profile from '../pages/Profile'
// import People from '../pages/People'
const routes = [
  {
    path:'/about',
    component:About,
    children:[
      // {name:'profile',path:'/about/year',component:Profile,meta:{isAuth:true}},
      // {name:'people',path:'/about/people',component:People,meta:{isAuth:true}},
      // {
      //   path:'/about',
      //   redirect:'/about/year'
      // },
]},
 {
  path:'/contaactus',
  component:ContaactUs
},
{
  path:'/persons',
  component:Persons,
  // children:[
  //   {
  //     path:'show/:id/:realname',component:Show,props:true
  //   // name:'show',  path:'show',component:Show
  //   }
  // ]
},
{
  path:'/',
  redirect:'/about'
},
]
 
const router = new VueRouter({
  mode:'history',
  routes
})
 
// router.beforeEach((to,from,next)=>{
 
//   if(to.name=="people" || to.name=="profile"){
// 		if(localStorage.getItem("token")=="123"){
// 			next();
// 		}
// 	}else{
// 		next();
// 	}
// })
 
// router.beforeEach((to,from,next)=>{
 
//   if(to.meta.isAuth){
// 		if(localStorage.getItem("token")=="123"){
// 			next();
// 		}
// 	}else{
// 		next();
// 	}
// })
 
export default router

以上就能實現(xiàn),視屏上的的切換的路由效果,如果有不懂的,私信問我,源碼私聊免費提供

三、嵌套路由

1.布局邏輯

嵌套路由在,最開始的路由下,加入路由

a5237959bdc84360928dbe8307e09c3b.png

在about路由組件中

debd2a4fd2de4376b79342945e6bb077.png

再次創(chuàng)建兩個路由組件,點擊是,獲得相對應的內(nèi)容,實現(xiàn)路由效果

552a993e9559482fbf63b63523e037c6.png

2.效果展示

13991d61fd614a66969234e28d7d068f.gif

3.代碼

about

<template>
<div>
    <!-- <div class="left"> -->
    <ul class="nav">
	<li><router-link to="/about/year" active-class="hover">創(chuàng)建年份</router-link></li>
	<li><router-link to="/about/people" active-class="hover">創(chuàng)建人</router-link></li>
    </ul>  
    <!-- </div> -->
   <keep-alive include="People">
    <router-view class="c"></router-view>
   </keep-alive> 
	
 
</div>
</template>
 
<script>
export default {
    name: 'About',
 
    data() {
        return {
            
        };
    },
 
    mounted() {
        
    },
 
    methods: {
        
    },
};
</script>
 
<style scoped>
 
</style>

兩個路由組件

Profile

<template>
<div>
    2002 08-20               
</div>
</template>
 
<script>
export default {
    name:'Profile',
    // beforeDestroy () {
    //     console.log('已銷毀');
    // },
    
}
</script>
 
<style>
 
</style>

People

<template>
  <div>
      <span>傅小余</span> <input type="text"> 
  </div>
</template>
 
<script>
export default {
name:"People",
 
// beforeDestroy () {
//   console.log('已銷毀');
// },
}
</script>
 
<style>
 
</style>

四、注意

這里我都使用到了默認路徑,所以頁面點開就會有展示效果

代碼如下

第一個里面的默認

{
  path:'/',
  redirect:'/about'
},

第二個

 {
        path:'/about',
        redirect:'/about/year'
      },

到此這篇關于詳解vue route介紹、基本使用、嵌套路由的文章就介紹到這了,更多相關vue route嵌套路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue中的v-cloak使用解讀

    Vue中的v-cloak使用解讀

    本篇文章主要介紹了Vue中的v-cloak使用解讀,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • 解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory

    這篇文章主要介紹了解決Vue使用百度地圖BMapGL內(nèi)存泄漏問題?Out?of?Memory,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue3.0中的computed寫法

    vue3.0中的computed寫法

    這篇文章主要介紹了vue3.0中的computed寫法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue2.0利用vue-resource上傳文件到七牛的實例代碼

    Vue2.0利用vue-resource上傳文件到七牛的實例代碼

    本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • vue中預覽zip的實現(xiàn)示例

    vue中預覽zip的實現(xiàn)示例

    打包壓縮成zip的東西,再解壓,很麻煩,本文主要介紹了vue中預覽zip的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法

    基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法

    這篇文章主要給大家介紹了基于Ant-design-vue的Modal彈窗封裝命令式與Hooks用法,文中有詳細的代碼示例,具有一定的參考價值,感興趣的同學可以借鑒閱讀
    2023-06-06
  • Vue.js實現(xiàn)動畫與過渡效果的示例代碼

    Vue.js實現(xiàn)動畫與過渡效果的示例代碼

    在現(xiàn)代前端開發(fā)中,用戶體驗至關重要,一個精美的動畫過渡不僅能提升界面的美觀性,還能讓用戶在使用時感受到流暢的交互體驗,在本文中,我們將深入探討如何在 Vue.js 中實現(xiàn)動畫與過渡效果,并提供示例代碼,需要的朋友可以參考下
    2024-10-10
  • Vue表單預校驗 validate方法不生效問題

    Vue表單預校驗 validate方法不生效問題

    這篇文章主要介紹了Vue表單預校驗 validate方法不生效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue項目如何讓局域網(wǎng)ip訪問配置設置

    vue項目如何讓局域網(wǎng)ip訪問配置設置

    這篇文章主要介紹了vue項目如何讓局域網(wǎng)ip訪問配置設置,具有很好的參考價值,希望對大家有所幫助。
    2022-09-09
  • vue可視化表單設計器自定義組件使用方法

    vue可視化表單設計器自定義組件使用方法

    Vue前端開發(fā)中表單組件是排在前三的高頻使用的組件,下面這篇文章主要給大家介紹了關于vue可視化表單設計器自定義組件使用的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12

最新評論

宝鸡市| 莆田市| 宜兰市| 兴山县| 南郑县| 佛教| 安仁县| 承德市| 阿巴嘎旗| 十堰市| 武穴市| 濮阳市| 东平县| 万州区| 中牟县| 崇仁县| 宾阳县| 宝丰县| 姜堰市| 大庆市| 循化| 四会市| 佛山市| 海门市| 阿克| 江油市| 庄河市| 昌平区| 汕尾市| 富裕县| 高碑店市| 梁河县| 昆山市| 彭水| 大竹县| 龙口市| 苗栗县| 无锡市| 南江县| 澜沧| 汝阳县|