vue的路由跳转与路由嵌套

本文详细介绍了Vue项目中路由的工作原理及应用,包括vue-router的安装、配置与使用,以及路由跳转和嵌套的实现方式,帮助开发者掌握Vue中页面导航的高效技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:https://blog.youkuaiyun.com/Mr_JavaScript/article/details/80744268

路由的原理是通过改变网址,来实现页面的局部刷新,相比a标签跳转的不同之处在于,路由跳转不需要刷新整个页面。

一、路由跳转

1、安装路由vue-router:

npm install vue-router

 

2、vue项目引入vue-ruoter:

3、配置路由(页面跳转):

可以建一个专门用于路由的js文件,里面配置路径。

1)router.js路由配置文件

2)main.js里引入router.js路由文件

4、组件里调用

1)使用router-view标签给vue组件的跳转提供一个容器

2)使用router-link标签实现跳转(它类似于a标签,区别在于router-link跳转不需要刷新页面)

方法一:to里填写的是跳转的路径,即定义路由时的path路径

方法二:to里使用路由名称name跳转

跳转至table组件:

3、实现效果

项目的首页:

点击table后跳转:

(完成)

 

二、路由嵌套

1、配置嵌套路由


 
  1. {
  2. path: '/about', //一级路由
  3. component:About,
  4. children:[
  5. { //二级路由
  6. path: '/', //二级的默认路由(此path指向上一级,即path: '/about'
  7. name: 'expressLink',
  8. component:Express
  9. },
  10. {
  11. path: '/about/guide',
  12. name: 'guideLink',
  13. component:Guide
  14. },
  15. {
  16. path: '/about/contact',
  17. name: 'contactLink',
  18. component:Contact,
  19. children:[
  20. { //三级路由
  21. path: '/about/contact/personName',
  22. name: 'personNameLink',
  23. component:PersonName,
  24. },
  25. {
  26. path: '/about/contact/phone',
  27. name: 'phoneLink',
  28. component:Phone
  29. },
  30. ]
  31. }
  32. ]
  33. },

2、组件中嵌套使用<router-view></router-view>

1)一级路由

2)二级路由

3)三级路由

3、实现效果

(完)

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值