【Vue 路由(vue—router) 一】介绍,前端开发技巧

23 阅读3分钟
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')

二、基本使用(代码后赋)

以下例子展现路由的基本使用

css样式已经写好了,直接实现路由效果

展示效果

首先学习的效果

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">联系方式</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">创建年份</router-link></li>
	<li><router-link to="/about/people" active-class="hover">创建人</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>
        联系方式
    </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

以上就能实现,视屏上的的切换的路由效果,如果有不懂的,私信问我,源码私聊免费提供

三、嵌套路由

1.布局逻辑

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

a5237959bdc84360928dbe8307e09c3b.png

在about路由组件中

debd2a4fd2de4376b79342945e6bb077.png

再次创建两个路由组件,点击是,获得相对应的内容,实现路由效果

552a993e9559482fbf63b63523e037c6.png

2.效果展示

13991d61fd614a66969234e28d7d068f.gif

 3.代码

about

<template>
<div>
    <!-- <div class="left"> -->
    <ul class="nav">
	<li><router-link to="/about/year" active-class="hover">创建年份</router-link></li>
	<li><router-link to="/about/people" active-class="hover">创建人</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>


### 最后

**小编的一位同事在校期间连续三年参加ACM-ICPC竞赛。从参赛开始,原计划每天刷一道算法题,实际上每天有时候不止一题,一年最终完成了 600+:**

**凭借三年刷题经验,他在校招中很快拿到了各大公司的offer。**



**入职前,他把他的刷题经验总结成1121PDF书籍,作为礼物赠送给他的学弟学妹,希望同学们都能在最短时间内掌握校招常见的算法及解题思路。**

![](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/d34d7d1cfb6648e5a06cf994cf510fc0~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771407020&x-signature=FCcUi4xJT5UNrOR3uZuaTSxy1Zo%3D)

**整本书,我仔细看了一遍,作者非常细心地将常见核心算法题和汇总题拆分为4个章节。**

![](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/7f77440f8432488599d2310266c13825~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771407020&x-signature=SZbhnYcBdywvCu7kzIPVHs0H6rU%3D)



**而对于有时间的同学,作者还给出了他结合众多数据结构算法书籍,挑选出的一千多道题的解题思路和方法,以供有需要的同学慢慢研究。**

![](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/0d5c19b87074475292bd557f8e30c3e7~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3NTc5MjMwMTY3MDI=:q75.awebp?rk3s=f64ab15b&x-expires=1771407020&x-signature=n1iGwvMOgBHX0nfeProHeDVWqAI%3D)
**开源分享:https://docs.qq.com/doc/DSmRnRGxvUkxTREhO**