当前位置:首页 > 技术文章 > 正文内容

Vue Router用法详解,使用Vue Router做单页面应用

zonemu2个月前 (07-26)技术文章20

最近需要做一个单页面应用,于是就用到了Vue Router。简直是太好用了。就写了一篇文章记录了一下。

Vue Router 是 Vue.js 官方的路由管理器,它允许构建单页面应用,使得用户在不同视图间切换而无需重新加载页面。

用法详解

  1. 安装 Vue Router
  2. 通过 npm 安装 Vue Router:
npm install vue-router
  1. 确保安装的版本与 Vue.js 的版本兼容。
  2. 配置基础路由

1创建路由配置文件(通常是 router/index.js 或 router.js),定义路由和组件的映射关系:

import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../components/Home.vue';
import About from '../components/About.vue';

Vue.use(VueRouter);

const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];

const router = new VueRouter({
mode: 'history',
routes
});

export default router;
在 main.js 中引入并使用路由:import Vue from 'vue';
import App from './App.vue';
import router from './router';

new Vue({
router,
render: h => h(App)
}).$mount('#app');
在 App.vue 中使用 <router-link> 和 <router-view>:<template>
<div id="app">
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
<router-view></router-view>
</div>
</template>
  1. 通过 <router-link> 创建导航链接,<router-view> 展示当前路由对应的组件。
  2. 动态路由配置
  3. 动态添加路由,例如在用户登录后添加个人资料页面:
import Profile from '@/components/Profile.vue';
router.addRoute({
path: '/profile',
name: 'Profile',
component: Profile
});
  1. 动态添加嵌套路由:
router.addRoute('ParentRouteName', {
path: 'child',
name: 'ChildRouteName',
component: () => import('@/components/Child.vue')
});
  1. 动态路由完整实现示例:
// main.js
import Vue from 'vue';
import App from './App.vue';
import router from './router';

Vue.config.productionTip = false;
new Vue({
router,
render: h => h(App)
}).$mount('#app');

// router.js
import Vue from 'vue';
import Router from 'vue-router';
import Home from '@/components/Home.vue';
import About from '@/components/About.vue';

Vue.use(Router);
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
});

export default router;

// 动态添加路由
function loadDynamicRoutes() {
const permissions = ['home', 'about', 'profile']; // 示例权限列表
permissions.forEach(permission => {
if (permission === 'profile') {
router.addRoute({
path: '/profile',
name: 'Profile',
component: () => import('@/components/Profile.vue')
});
}
});
}

// 调用函数加载动态路由
loadDynamicRoutes();
  1. 动态路由可以根据用户权限或其他条件动态添加。

使用场景

  1. 模块懒加载
  2. 对于大型应用,为了优化性能,可以按需加载不同模块的路由。
  3. 多级菜单动态生成
  4. 在后台管理系统中,菜单项和对应的路由可能是动态生成的。可以根据后台返回的菜单配置,动态生成对应的路由。
  5. 权限控制
  6. 根据用户的权限动态加载不同的路由,例如只有管理员才能访问的页面。
  7. SPA(单页面应用)开发
  8. Vue Router 使得在单页面应用中实现多视图切换成为可能,无需重新加载整个页面。

创作不易,如果这篇文章对你有用,欢迎点赞关注加评论哦

小伙伴们在工作中还遇到过其他应用场景吗,欢迎评论区留言讨论哦。

相关文章

供热收费管理系统(供热收费管理系统有哪些)

今天来了解《#供热收费管理系统》,它包括如下功能:街道小区设置表格界面,热费标准设置表格界面,优惠设置表格界面,滞纳金设置表格界面,余热比例设置表格界面,用户录入信息窗口,用户档案表格界面,用户录入信...

Vue基础(vue基础组件文件名符合规范的是)

Vue 是什么,它的核心特点有哪些?Vue 是一款渐进式 JavaScript 框架,它基于标准 HTML、CSS 和 JavaScript 构建,并提供了一套声明式的、组件化的编程模型,帮助开发者用...

程序员开发必会之git常用命令,git配置、拉取、提交、分支管理

整理日常开发过程中经常使用的git命令!git配置SSH刚进入项目开发中,我们首先需要配置git的config、配置SSH方式拉取代码,以后就免输入账号密码了!# 按顺序执行 git config -...

程序员效率提升!使用自动化工具gitx,每周节约半小时

你是否经历过这样的折磨?一个 JIRA 需求要同时修复 dev、qa、staging 三个分支每个版本涉及 A、B、C 三个项目手动执行以下操作:从 dev 切临时分支cherry-pick 提交推送...

如何使用git clone克隆包含子模块的仓库

技术背景在软件开发中,当项目规模逐渐增大时,为了更好地管理代码,我们常常会使用子模块将项目拆分成多个独立的部分。Git 作为目前最流行的版本控制系统,提供了管理子模块的功能。但在克隆包含子模块的仓库时...

100行Html5+CSS3+JS代码实现元旦倒计时界面

一、前言2022年到了,祝大家虎年大吉喜气临,昂首摆尾迎春来。双眼圆睁看世界,万水千山尽开颜。胡须翘翘美姿态,人人开心祝平安。巨大身躯摇摆摆,坎坷困境当笑谈。愿你虎年万事顺,吉星高照旺旺旺!二、202...