当前位置: 首页 > news >正文

前端路由VueRouter总结

简介:

Vue路由vue-router是官方的路由插件,能够轻松的管理 SPA 项目中组件的切换。Vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来vue-router 目前有 3.x 的版本和 4.x 的版本,vue-router 3.x 只能结合 vue2进行使用,vue-router 4.x 只能结合 vue3 进行使用

1.VueRouter的安装和引入

vue2版本执行命令

npm install vue-router@3.0.1

vue3版本执行命令

npm install vue-router@4

创建router文件夹和index.js文件

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Discover from '@/components/Discover'
import My from '@/components/My'
import Friends from '@/components/Friends'Vue.use(Router)export default new Router({routes: [{path: '/',name: 'HelloWorld',component: HelloWorld},{path: '/discover',component: Discover},{path: '/my',component: My},{path: '/friend',component: Friends},]
})

在main.js中添加代码引入挂载router

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'Vue.use(ElementUI);//配置请求根路径
axios.defaults.baseURL="http://127.0.0.1:8789/";//将axios作为全局的自定义属性,每个组件可以在内部直接访问
Vue.prototype.$http = axios;Vue.config.productionTip = false;/* eslint-disable no-new */
new Vue({el: '#app',router,components: { App },template: '<App/>'
})

2.VueRouter的常规使用方法

VueRouter的路由导航

创建Discover.vue,Friends.vue,My.vue并在App.vue使用导航显示

Discover.vue

<template><div><h1>发现音乐</h1></div>
</template> 

Friends.vue

<template><div><h1>关注</h1></div>
</template> 

My.vue

<template><div><h1>我的音乐</h1></div>
</template> 

App.vue

<template><div id="app"><!-- 声明路由链接 --><router-link to="/discover">发现音乐</router-link><router-link to="/my">我的音乐</router-link><router-link to="/friend">关注</router-link><!-- 声明路由占位标签 --><router-view/></div>
</template><script>
import First from '@/components/First.vue'
import Movie from '@/components/Movie.vue'
import Table from '@/components/Table.vue'
export default {name: 'App',data:function(){return {movies:[{id:1,title:"金刚狼",rating:"8.8"},{id:2,title:"金刚狼2",rating:"8.9"},{id:3,title:"金刚狼3",rating:"9.0"},],tbdata:[]}},components:{First,Movie,Table}
}
</script><style>
#app {font-family: 'Avenir', Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
}
</style>

效果:

首页重定向的使用

修改router/index.js的首页路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Discover from '@/components/Discover'
import My from '@/components/My'
import Friends from '@/components/Friends'Vue.use(Router)export default new Router({routes: [{path: '/',redirect:'/discover'},{path: '/discover',component: Discover},{path: '/my',component: My},{path: '/friend',component: Friends},]
})

 这一进入首页后默认重定向到/dicover路由

效果:

嵌套路由的使用

 在Discover.vue中新建子路由链接及占位显示标签

<template><div><h1>发现音乐</h1><!-- 子路由链接 --><router-link to="/discover/toplist">推荐</router-link><router-link to="/discover/playlist">歌单</router-link><hr><router-view/></div>
</template> 

 新建需要显示的子模块TopList.vue和PlayList.vue

TopList.vue

<template><div><h2>推荐</h2></div>
</template> 

PlayList.vue

<template><div><h2>歌单</h2></div>
</template> 

在router/index.js中通过children属性,嵌套声明子路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Discover from '@/components/Discover'
import My from '@/components/My'
import Friends from '@/components/Friends'
import TopList from '@/components/TopList'
import PlayList from '@/components/PlayList'Vue.use(Router)export default new Router({routes: [{path: '/',redirect:'/discover'},{path: '/discover',component: Discover,//通过children属性,嵌套声明子路由children:[{path:"toplist",component:TopList},{path:"playlist",component:PlayList}]},{path: '/my',component: My},{path: '/friend',component: Friends},]
})

效果:

动态路由

 

 编程式导航

导航守卫 

http://www.lryc.cn/news/421607.html

相关文章:

  • 基于SpringBoot+Vue的铁路订票管理系统(带1w+文档)
  • Firefox滚动条在Win10和Win11下表现不一致问题?
  • vue3 组件传参
  • unity自动添加头部注释脚本
  • Raw格式化后文件能恢复吗 电脑磁盘格式化后如何恢复数据 硬盘格式变成了raw怎么恢复
  • Android targetSdkVersion改成33遇到的坑
  • 1985-2023年中国城市统计年鉴(PDF+EXCEL)
  • 从AI小白到大神的7个细节:让你开窍逆袭
  • AIxBoard部署BLIP模型进行图文问答
  • 小白零基础学数学建模应用系列(一):探索自由下落模型——以“坠落的硬币”为例
  • linux主机间免密登录
  • 【海思SS626 | VB】关于 视频缓存池 的理解
  • RCE漏洞及绕过
  • 非对称加密算法-ECDHE
  • 10分钟学会Docker的安装和使用
  • 江科大/江协科技 STM32学习笔记P20
  • CSS 实现两边固定宽,中间自适应
  • C#图片批量下载Demo
  • 部署Springboot + Vue 项目到远程服务器Windows10系统的详细配置
  • 智驭灌区,科技领航—— 高效灌区信息化系统管理平台
  • 下载免费设计素材,有这7个网站就够了
  • 【漏洞复现】某赛通数据泄露防护(DLP)系统 NetSecConfigAjax SQL注入漏洞
  • c++中的仿函数
  • springboot整合mybatis-plus和pagehelper插件报错,
  • 趋动科技荣登「AIGC赋能金融创新引领者TOP20」
  • SOPHGO算能科技BM1684盒子占用空间满的问题解决
  • Spring Boot实用小技巧 - - 第523篇
  • 安卓App开发 篇二:Android UI和布局
  • k8s基本介绍
  • go http启动应用程序