1天搞定SpringBoot+Vue全栈开发 (8)前端路由VueRouter(进行组件切换)

本文主要是介绍1天搞定SpringBoot+Vue全栈开发 (8)前端路由VueRouter(进行组件切换),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.VueRouter安装与使用

2.参数传递

创建路由组件

在项目中定义Discover.vue、Friends.vue、My.vue三个组件,将来要使用vue-router来控制它们的展示与切换:

Discover.vue

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

Friends.vue

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

My.vue

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

声明路由链接和占位标签

可以使用<router-link> 标签来声明路由链接,并使用<router-view> 标签来声明路由占位符

App.vue:

<template><div id="app"><!-- 声明路由链接 --><router-link to="/discover"> 发现音乐 </router-link><router-link to="/my"> 我的音乐 </router-link><router-link to="/friends"> 关注音乐 </router-link><!-- 声明路由展位标签 --><router-view></router-view></div>
</template><script>
export default {name: "App",components: {},
};
</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>

Main.js:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = falsenew Vue({render: h => h(App),router: router
}).$mount('#app')

Index.js(在新建的src下面的router文件夹里) 

import VueRouter from "vue-router";
import Vue from "vue";
import Discover from "../components/Discover.vue"
import Friends from "../components/Friends.vue"
import My from "../components/My.vue"Vue.use(VueRouter);const router = new VueRouter({// 指定hash属性与组件的对应关系routes: [{ path: "/discover", component: Discover },{ path: "/friends", component: Friends },{ path: "/my", component: My },],
})
export default router;

测试:

 

3.子路由

嵌套路由 

Discover.vue

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

TopList.vue

<template><h3>推荐</h3>
</template>

PlayList.vue

<template><h3>歌单</h3>
</template>

 index.js:

import VueRouter from "vue-router";
import Vue from "vue";
import Discover from "../components/Discover.vue";
import Friends from "../components/Friends.vue";
import My from "../components/My.vue";
import TopList from "@/components/TopList.vue";
import PlayList from "@/components/PlayList.vue";Vue.use(VueRouter);const router = new VueRouter({// 指定hash属性与组件的对应关系routes: [{ path: "/", redirect: "/discover" },{path: "/discover",component: Discover,//通过children属性,嵌套声明子路由children: [{ path: "toplist", component: TopList },{ path: "playlist", component: PlayList },],},{ path: "/friends", component: Friends },{ path: "/my", component: My },],
});
export default router;

测试:

动态路由

动态路由指的是:把 Hash 地址中可变的部分定义为参数项,从而提高路由规则的复用性。在 vue-router 中使用英文的冒号(:)来定义路由的参数项:

{path:'/product/:id',component:Product}

Product.vue:

<template><h3>商品</h3>
</template>

My.vue:

import VueRouter from 'vue-router';
<template><div><h1>我的</h1><router-link to="/my/1">商品1</router-link><router-link to="/my/2">商品2</router-link><router-link to="/my/3">商品3</router-link><VueRouter></VueRouter></div>
</template>

index.js:

{ path: "/my", component: My },

改成

  {path: "/my",component: My,children: [{ path: ":id", component: Product }],},

或者将Producr.vue改成:

<template><div><h3>商品{{ $route.params.id }}</h3> </div>
</template><script>
export default {props: ["id"],
};
</script>

4.导航守卫

控制路由的访问权限

全局导航守卫会拦截每个路由规则,从而对每个路由进行访问权限的控制.

你可以使用 router.beforeEach 注册一个全局前置守卫

router.beforeEach((to, from, next) => {if (to.path==='/main'&&!isAuthenticated) {next('/login');} else {next();}
});

to:即将要进入的目标
from:当前导航正要离开的路由
在守卫方法中如果声明了 next 形参,则必须调用 next() 函数,否则不允许用户访问任何一个路
        直接放行:next()
        强制其停留在当前页面: next(false)
        强制其跳转到登录页面: next('/login')

这篇关于1天搞定SpringBoot+Vue全栈开发 (8)前端路由VueRouter(进行组件切换)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/969474

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template