内容简介:路由的作用:在web端路由(route)就是URL到函数的映射,vue的router就像一个容器,分配,处理每一个route到URL中。文档地址:
上篇我们说了vue项目的目录设计,本篇我们来学习一下vue路由。
路由的作用:
在web端路由(route)就是URL到函数的映射,vue的router就像一个容器,分配,处理每一个route到URL中。
文档地址:
vue路由官方文档https://router.vuejs.org/zh/guide/#html
安装:
-
通过node.js安装;
npm install vue-router
- 但是一般情况下在node中安装vue项目的时候根据提示选择安装vue-router;
如何使用路由:
举个项目例子:
1.添加路由链接,打开Nav.vue
<router-link to="/goods" class="nav-item">点菜</router-link>
<router-link to="/rates" class="nav-item">评价</router-link>
<router-link to="/seller" class="nav-item">商家</router-link>
to是路由指向的地址。
2.决定将组建渲染在哪,打开App.vue,添加:
<router-view/>
3.配置路由,打开router文件夹下index.js
import Vue from 'vue'
import Router from 'vue-router'
//1.引入各个组件
import Goods from '@/components/Goods/Goods'
import Rates from '@/components/Rates/Rates'
import Seller from '@/components/Seller/Seller'
Vue.use(Router)
//2.配置路由 路径->组件
export default new Router({
routes: [
{
path: '/',
redirect:'/goods'
},
{
path: '/goods',
component: Goods
},
{
path: '/rates',
component: Rates
},
{
path: '/seller',
component: Seller
}
],
linkActiveClass:'active'
})
redirect路由重定向:
我们在router数组设定redirect为“/goods”。那么只要路径是/。页面会跳转到"/goods"页面。
redirect官方文档:
https://cn.vuejs.org/v2/guide...
注意,当前激活导航设置方法:
linkActiveClass:'active'
linkActiveClass:固定属性;
‘active’:值。是我们定义的class类;
我们在点击评价的时候,会得到上图中的效果。
4.路由的keep-alive
针对于上图中的业务情况:
- 我们在点菜页面;
- 点了些食品;
- 那么现在购物车内会有我们点过的数据;
-
点击其他页面,数据需要得到保留。
所以使用keep-alive保留组件状态,避免重新渲染,购物车的数据丢失。
<keep-alive> <router-view ></router-view> </keep-alive>
keep-alive官方文档: https://cn.vuejs.org/v2/api/#...
总结
我们从安装,到组件配置路由再到使用keep-alive的业务场景复现,让我们对vue的router相关的技术点有了一定了解,我们将一步步完善项目,丰富我们的技术经验。下篇我们通过axios为项目添加数据交互功能,敬请关注。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 【译】RabbitMQ 实战教程(四) 路由
- Golang Gin 实战(三):路由参数
- React框架Umi实战(3)路由进阶
- 路由器0day漏洞挖掘实战
- Knative 实战:如何在 Knative 中配置自定义域名及路由规则?
- vue路由篇(动态路由、路由嵌套)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Game Programming Patterns
Robert Nystrom / Genever Benning / 2014-11-2 / USD 39.95
The biggest challenge facing many game programmers is completing their game. Most game projects fizzle out, overwhelmed by the complexity of their own code. Game Programming Patterns tackles that exac......一起来看看 《Game Programming Patterns》 这本书的介绍吧!