vue-router 进阶

vue-router 进阶

1.动态路由

​ url中路由是改变的,可是改变路由公用一个组件javascript

举例:前端

  1. http://localhost:8080/category/detail/001?a=1&b=1
  2. http://localhost:8080/category/detail/002?a=2&b=2
  3. http://localhost:8080/category/detail/003?a=3&b=3

页面中的配置:vue

<router-link class="nav-link " active-class='active' href="#" 

:to="{name:'detail',params:{id:'001'},query:{a:1,b:1}}" //在router-link标签里面设置

>数码</router-link>

路由配置(这里是二级路由)java

{
        path: '/category',
        component: Category,
        children: [{
            path: 'detail/:id', //:id表示能够匹配/category后的全部参数,
            //举例:http:localhost:8080/category/001?a=1&b=2hash=3 那么:id就表明001开始日后的部分
            component: Detail,
            name: 'detail'      //这里是命名路由
        }]
    },

注意:因为动态路由url中会携带参数 能够用于发送数据请求 来获取对应的数据webpack

那么在组件中如何获取url中携带的参数?ios

在vue生命周期created中来获取数据 在vue的实例对象中存在$router里面的params,query属性就是url中的值web

<template>
    <div>
        <p>id:{{$route.params.id}}</p>
        <p>query.a:{{$route.query.a}}</p>
        <p>query.b:{{$route.query.b}}</p>
    </div>
</template>
<script>
export default {
    created(){
        console.log(this)
    }
}
</script>

案例:ajax

获取堆糖网站上的信息渲染到页面vue-router

首先在项目中建立vue.config.js (用来作前端的反向代理)axios

module.exports = {
    devServe: {
        proxy: {
            '/napi': {
                target: 'https://m.duitang.com',
                changeOrigin: true
            }
        }
    }
}

利用axios请求网站上的数据(注意引入axios组件)

如今入口文件设置全局axios

import axios from 'axios'
Vue.prototype.$http = axios
<script>
    export default {
        data(){
            return {
                navs:null
            }
        },
        created(){
            this.$http({
                url:'/napi/category/detail/',
                params:{
                   app_version:14,
                    app_code:'mdt',
                    category_id:'5017d172705cbe10c0000007',
                }
            }).then(res => {
              //将请求得来的数据 赋值给 当前组件的navs
              this.navs = res.data.data.sube_cates

            })
              .catch(error => console.log(error))
        }
    }
</script>

最后在zxh上接受数据渲染到页面

<template>
  <div>
    <ul>
      <li v-for = " list in lists " :key = "list.id">
        <p> 点赞量:{{ list.favorite_count }} </p>  \\随便取数据渲染到页面
        <p> msg: {{ list.msg }} </p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      lists: null
    }
  },
  watch: {
    $route () {
      this.ajax()
    }
  },
  methods: {
    ajax () {
      this.$http({
        url: '/napi/blog/list/by_category/',
        params: {
          start: 0,
          include_fields: 'sender,album,like_count,msg',
          limit: 24,
          cate_key: this.$route.params.id,
          path: this.$route.query.path,
          _: Date.now()
        }
      }).then( res => {
        this.lists = res.data.data.object_list
      })
        .catch( error => console.log( error ))
      }
  }
}
</script>

2.路由懒加载

因为每次点击都至关于发起一次请求 极大增长了浏览器的性能,因此须要使用懒加载来减小请求的次数

在路由配置文件中设置

const Home = () => import(/* webpackChunkName: "group-foo" */ '../pages/Home.vue')

这种形式的导入模块方式就是路由懒加载

相关文章
相关标签/搜索