2021SC@SDUSC
一、引言
本篇文章将对老年照护健康知识图谱项目中使用的参数传递进行分析。
二、代码分析
1. Demo
1.前端传递参数
此时我们在Main.vue中的route-link位置处 to 改为了 :to,是为了将这一属性当成对象使用,注意 router-link 中的 name 属性名称 一定要和 路由中的 name 属性名称 匹配,因为这样 Vue 才能找到对应的路由路径;
<!--name:传组件名 params:传递参数,需要绑定对象:v-bind-->
<router-link v-bind:to="{name: 'UserProfile', params: {id: 1}}">个人信息</router-link>
2.修改路由配置,增加props:true属性
主要是router下的index.js中的 path 属性中增加了 :id 这样的占位符
{
path: '/user/profile/:id',
name: 'UserProfile',
component: UserProfile,
props:true
}
3.前端显示
在要展示的组件Profile.vue中接收参数
Profile.vue:
<template>
<div>
个人信息
{{ id }}
</div>
</template>
<script>
export default {
props: ['id'],
name: "UserProfile"
}
</script>
<style scoped>
</style>
2.组件重定向
Vue 中的重定向是作用在路径不同但组件相同的情况下,比如:
在router下面index.js的配置
{
path: '/main',
name: 'Main',
component: Main
},
{
path: '/goHome',
redirect: '/main'
}
这里定义了两个路径,一个是 /main ,一个是 /goHome,其中 /goHome 重定向到了 /main 路径,由此可以看出重定向不需要定义组件。
使用时需要在Main.vue设置对应路径;
<el-menu-item index="1-3">
<router-link to="/goHome">回到首页</router-link>
</el-menu-item>
本文分析了老年照护健康知识图谱项目中使用的参数传递方法,并介绍了Vue中组件重定向的应用场景。通过实例展示了如何在Vue应用中利用router-link进行参数传递,以及如何配置路由实现组件间的重定向。

被折叠的 条评论
为什么被折叠?



