vue-router是vue中的一个核心插件。php
1.若是安装脚手架,那么能够npm install vue-router 便可安装css
而后导入而且引用就能够了。html
import VueRouter from 'vue-router' Vue.use(VueRouter)
2.也可下载vue-router.js ,而后直接script引用也能够。vue
用 Vue.js + vue-router 建立单页应用,是很是简单的。使用 Vue.js ,咱们已经能够经过组合组件来组成应用程序,当你要把 vue-router 添加进来,咱们须要作的是,将组件(components)映射到路由(routes),而后告诉 vue-router 在哪里渲染它们。ajax
2.1关于渲染标签vue-router
vue-router提供了两个指令标签组件来处理导航和自动渲染的逻辑vue-cli
<router-view> 渲染路径匹配到的视图组件,还能够内嵌本身的<router-view> ,根据嵌套路径渲染嵌套组件npm
<router-link> 路由中的应用导航api
2.2关于跳转app
使用router-link组件来进行导航,经过传入“to”属性指定连接
<router-link to="/home">Go to Home</router-link>
可是一样,咱们须要在js中配置路由的跳转
//定义好路由组件 //index.js 配置路由跳转 export default new Router({routes}) var routes = [ { path: '/home', name: 'Home', component: Home } ]
2.3 关于传值
页面跳转时常常须要携带参数,因此路由跳转是能够携带参数的
//动态路径传参,以冒号开头 var routes = [ { path: '/detail/:id', name: 'Detail', component: Detail } ]
固然在路由中也须要配置一下
<router-link :to='{name:"Detail",params:{id:x.name}}'> ... </router-link>
2.4 关于参数结接收
一个『路径参数』使用冒号 : 标记。当匹配到一个路由时,参数值会被设置到 this.$route.params,能够在每一个组件内使用。因而,咱们能够更新Detail的模板,输出当前用户的商品名
<template> <div> <h1>水果详情页</h1> <h1>{{$route.params.id}}</h1> </div> </template>
2.5 关于渲染
默认状况下,<router-link>会被渲染成a标签输出,而有时候,为了页面的规范和美观,咱们能够将其替换成其余标签,好比<router-link to:"/home" tag="div" > 最终,<router-link>就会以div的形式显示在页面上
2.6 嵌套式路由
上文说过,路由之间是能够嵌套的,因此咱们能够在路由中进行子路由的嵌套
那么此时,首先,咱们须要先配置路由
//此时,咱们在market组件下,配置2个子路由 var routes = [ { path: '/market', name: 'Market', component: Market, children: [ { path: '/', component: require('../pages/market/price') }, { path: 'price', component: require('../pages/market/price') }, { path: 'rank', component: require('../pages/market/rank') } ] } ] export default new Router({routes})
market组件中
<template> <div> <ul> <router-link to='/market/price' tag="li">每天特价</router-link> <router-link to='/market/rank' tag="li">热销榜</router-link> </ul> <router-view></router-view> </div> </template>
其实这样咱们对应的子路由就配置好了。
是否是有点懵,作一个系统的例子
1.根据笔者的第一篇文章,安装下vue-cli以及学习基本知识《Vue2.0(一,vue实例)》http://www.jianshu.com/p/d5272bd2db5e
2.根据笔者的第二篇vue文章,学习一下vue的基本指令《Vue2.0(vue基本指令)》http://www.jianshu.com/p/7a8f2ce9ef5e
3.看过度割线上的内容
要点
1.一级路由跳转及传参
2.子路由的配置和跳转
3.基本组件的使用和传参
麻雀虽小,可是涵盖不少知识点,基本等价于一个小的app的功能
3.1 构建页面组件
创建4个主页面组件以及1个详情页组件
3.1.1home.vue
//由于点击home.vue中的任何一个商品都跳转到商品详情页,因此在渲染每个商品的时候,咱们都加上router-link,而且进行跳转的传递参数 <template> <div> <ul> <li v-for='x in list'> <router-link :to='{name:"Detail",params:{id:x.name}}'> <div>  </div> <h3> {{x.name}}{{x.price}} </h3> </router-link> </li> </ul> </div> </template> <script> export default{ data(){ return { list:[] } }, beforeCreate(){}, created(){}, computed:{}, mounted(){ this.$http.get('http://www.vrserver.applinzi.com/aixianfeng/apihomehot.php') .then(response=>{ this.list = response.body.data; },response=>{ }); }, methods:{}, components:{} } </script> <style> ul>li{ display:block } </style>
3.1.2market.vue
<template> <div> <ul> <router-link to='/market/price' tag="li">每天特价</router-link> <router-link to='/market/rank' tag="li">热销榜</router-link> <router-link to='/market/milk' tag="li">牛奶面包</router-link> <router-link to='/market/fruit' tag="li">优选水果</router-link> </ul> <router-view></router-view> </div> </template> <script> export default{ data(){ return { list:[] } }, computed:{}, mounted(){}, methods:{}, components:{} } </script> <style> ul,li{ list-style: none; } *{ margin: 0; padding: 0; } .nav{ position: fixed; height: 100%; width:25%; background: #dfdfdf; list-style: none; text-align: center; left: 0; top: 0; } .footer{ position: fixed; height: 40px; width:100%; background: #dfdfdf; list-style: none; text-align: center; left: 0; bottom: 0; z-index:10; } .nav li{ /*width: 25%;*/ padding: 10px; } .nav a{ text-decoration: none; } .router-link-active{ background:skyblue } .view{ position: fixed; height: 100%; width:75%; background: #fff; list-style: none; text-align: center; right: 0; top: 0; overflow-y:auto } </style>
3.1.3car.vue和mine.vue简写
<template> <h1>个人</h1> </template> <script> export default{ data(){ return { } }, computed:{}, mounted(){ }, methods:{}, components:{} } </script> <style lang='css'> </style>
3.2组件引用和路由配置
安装好路由,而且进行组件的引入和路由的配置
import Vue from 'vue'import Router from 'vue-router'import VueResource from 'vue-resource'Vue.use(Router) Vue.use(VueResource)import Home from 'pages/home.vue'import Market from 'pages/market.vue'import Car from 'pages/car.vue'import Mine from 'pages/mine.vue'import Detail from 'pages/detail.vue'var routes = [ { path: '/', name: 'Home', component: Home }, { path: '/home', name: 'Home', component: Home }, { path: '/market', name: 'Market', component: Market, children: [ { path: '/', component: require('../pages/market/price') }, { path: 'price', component: require('../pages/market/price') }, { path: 'rank', component: require('../pages/market/rank') }, { path: 'milk', component: require('../pages/market/milk') }, { path: 'fruit', component: require('../pages/market/fruit') }, ] }, { path: '/car', name: 'Car', component: Car }, { path: '/mine', name: 'Mine', component: Mine }, { path: '/detail/:id', name: 'Detail', component: Detail } ]export default new Router({routes})
3.3detail.vue
点击home页中的每个商品都须要跳转到商品详情页,因此咱们要进行参数的接收和页面渲染
<template> <div> <h1>水果详情页</h1> <h1>{{$route.params.id}}</h1> </div> </template> <script> export default{ data(){}, computed:{}, mounted(){ }, methods:{}, components:{} } </script> <style lang='css'> </style>
3.4market.vue
在market组件中,咱们引用二级路由,因此须要定义router-link和router-view,由于每个二级路由须要渲染不一样的部分,好比每天特价,热销榜等等,因此咱们还须要这四个组件,看3.4
<template lang='html'> <div> <ul> <router-link to='/market/price' tag="li">每天特价</router-link> <router-link to='/market/rank' tag="li">热销榜</router-link> <router-link to='/market/milk' tag="li">牛奶面包</router-link> <router-link to='/market/fruit' tag="li">优选水果</router-link> </ul> <router-view></router-view> </div> </template> <script> export default{ data(){ return { list:[] } }, computed:{}, mounted(){}, methods:{}, components:{} } </script> <style lang='css'> ul,li{ list-style: none; } *{ margin: 0; padding: 0; } .nav{ position: fixed; height: 100%; width:25%; background: #dfdfdf; list-style: none; text-align: center; left: 0; top: 0; } .footer{ position: fixed; height: 40px; width:100%; background: #dfdfdf; list-style: none; text-align: center; left: 0; bottom: 0; z-index:10; } .nav li{ /*width: 25%;*/ padding: 10px; } .nav a{ text-decoration: none; } .router-link-active{ background:skyblue } .view{ position: fixed; height: 100%; width:75%; background: #fff; list-style: none; text-align: center; right: 0; top: 0; overflow-y:auto } </style>
3.5 market的四个组件
3.5.1 fruit.vue
<template> <List type='优选水果'></List> </template> <script> import List from '../../components/List' export default{ data(){ return { } }, computed:{}, mounted(){ }, methods:{}, components:{ List } } </script> <style></style>
3.5.2 milk.vue
<template> <List type='牛奶面包'></List> </template> <script> import List from '../../components/List' export default{ data(){ return { } }, beforeCteate(){}, create(){}, computed:{}, mounted(){}, methods:{}, components:{ List } } </script> <style></style>
3.5.3price.vue
<template> <List type='每天特价'></List> </template> <script> import List from '../../components/List' export default{ data(){ return { } }, beforeCteate(){}, create(){}, computed:{}, mounted(){}, methods:{}, components:{ List } } </script> <style></style>
3.5.4rank.vue
<template> <List type='热销榜'></List> </template> <script> import List from '../../components/List' export default{ data(){ return { } }, beforeCteate(){}, create(){}, computed:{}, mounted(){}, methods:{}, components:{ List } } </script> <style></style>
仔细看着四个小组件,是否是都是一样引用了一个List.vue的组件,觉得若是这四个页面的结构是同样的,因此咱们只须要引用要给共同的组件便可
3.5.5 List.vue
<template> <div class="mod-home"> <ul> <li v-for='x in list'> <div class="">  </div> <h3> {{x.name}}{{x.price}} </h3> </li> </ul> </div> </template> <script> export default { data () { return { list:[] } }, props:['type'], computed:{}, mounted(){ var type = this.type || '每天特价'; this.$http.get('http://www.vrserver.applinzi.com/aixianfeng/apicategory.php?category='+type) .then(response=>{ this.list = response.body.data },response =>{ }) }, methods:{} } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: block; margin: 0 10px; } a { color: #42b983; } </style>
这里咱们在不一样的父组件传入一个type,在子组件List中,咱们接收type,而且根据type不一样,请求不一样的ajax便可。
这样咱们一个小案例就能够正常运行了。
这篇文章真心耗时,噗,单身妹子该出去浪才对,伤不起~啦啦啦
文章不定时更新~
喜欢就收藏,真爱就打赏~笔芯~