• vue路由简介和基础使用


    目录

    1、Vue Router介绍

    2、案例介绍

    2.1 需要安装哪些包

    2.2 布局 

    3.3 vue-router使用

    3.4 axios的使用 

    3.5 其它


    #博学谷IT学习技术支持#

    1、Vue Router介绍

    Vue Router 是 Vue.js 的官方路由。它与 Vue.js 核心深度集成,让用 Vue.js 构建单页应用变得轻

    而易举。

    前端路由作用: 实现业务场景切换

    优点:

    • 整体不刷新页面,用户体验更好

    • 数据传递容易, 开发效率高

    缺点:

    • 开发成本高(需要学习专门知识)

    • 首次加载会比较慢一点。不利于seo  

    2、案例介绍

     本次利用路由实现下面的移动端的案例:

     

    2.1 需要安装哪些包

    vue脚手架就不用介绍了

    这里用的是vue2版本

    安装:vue-router 这里安装版本3的

    yarn add vue-router@3.0.6

    下载axios包

    下载vant2包 

    2.2 布局 

    在vant中找到对应的组件,通过按需引入组件,完成页面的布局

    这里看一下底部布局:

    1. <template>
    2. <div class="main">
    3. <van-nav-bar :title="activeTitle" fixed />
    4. <van-tabbar route>
    5. <van-tabbar-item replace to="/home" icon="home-o">主页van-tabbar-item>
    6. <van-tabbar-item replace to="/study" icon="guide-o">学习中心van-tabbar-item>
    7. <van-tabbar-item replace to="/my" icon="user-circle-o">我的van-tabbar-item>
    8. van-tabbar>
    9. <router-view />
    10. div>
    11. template>

     对应的就是下图:

     现在我们需要路由进行页面的切换

    3.3 vue-router使用

    在src下创建一个routes文件夹,然后在创建两个js文件

    如下;

    1. 在routes.js文件下,主要是创建规则数组

    1. //导入组件 路由跳转后进入的页面
    2. import Home from "@/views/Home"
    3. import Study from "@/views/Study"
    4. import My from "@/views/My"
    5. //定义规则数组
    6. const routes = [
    7. {
    8. path: "/",
    9. //重定向
    10. redirect: "/home"
    11. },
    12. {
    13. path: "/home",
    14. component: Home,
    15. meta: {
    16. title: "首页"
    17. }
    18. },
    19. {
    20. path: "/study",
    21. component: Study,
    22. meta: {
    23. title: "学习中心"
    24. }
    25. },
    26. {
    27. path: "/my",
    28. component: My,
    29. meta: {
    30. title: "我的"
    31. }
    32. }
    33. ]
    34. export default routes

    在index.js文件下:

    1. import routes from "./routes"
    2. import VueRouter from 'vue-router'
    3. import Vue from 'vue'
    4. Vue.use(VueRouter)
    5. const router = new VueRouter({
    6. routes
    7. })
    8. export default router

    最后在main.js下关联到vue实例

    那么路由就创建好了

    总结一下:

     路由的使用也就下面几步

     3.4 axios的使用 

    这个看文档就行

    request.js:

    1. //导入axios
    2. import axios from 'axios'
    3. //准备接口的基本路径,以后就不用写了
    4. axios.defaults.baseURL = 'http://toutiao.itheima.net';
    5. //导出
    6. export default axios

    在api文件下,就是写请求了:

    1. import request from '@/utils/request'
    2. export const getList = () => {
    3. return request({
    4. url: '/v1_0/user/channels',
    5. })
    6. }

    3.5 其它

    关于其他的细节自己区vant2文档了解就好 

     

  • 相关阅读:
    androidStudio第一次运行报错无法运行
    C++之内存泄漏
    元宇宙|高阶音频处理能力,让声音「声临其境」
    PSPACE完全性学习笔记
    雅思写作拾遗01----图表类作文
    c#调用摄像头进行二维码扫码
    路径规划算法:基于果蝇优化的路径规划算法- 附代码
    为什么做生意可以让双方生活的更好?
    1-D Fractional Brownian Motion Synthesis
    dubbo与springboot的集成和使用dubbo-spring-boot-starter
  • 原文地址:https://blog.csdn.net/Lotus_fragrance/article/details/126184789