第十四篇 - vue页面跳转参数传递
本文介绍的一个vue页面调到第二个vue页面时,如何传递参数。
参考链接:https://blog.csdn.net/m0_73493077/article/details/126906574
第一步:先修改LoginPage.vue,跳转携带参数
/* eslint-disable */ <template> <div class="login_container"> <div class="login_box"> <div class="wai"> <!-- 头像区域 --> <div class="avatar_box"> <img src="../assets/head.png" alt="" /> </div> <!-- 登录表单区域 --> <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form" > <!-- 用户名--> <el-form-item prop="username"> <label class="form-label">用户名</label> <el-input v-model="loginForm.username" prefix-icon="User" ></el-input> </el-form-item> <!-- 密码--> <el-form-item prop="password"> <label class="form-label">密码</label> <el-input v-model="loginForm.password" prefix-icon="Key" type="password" ></el-input> </el-form-item> <!-- 按钮区域--> <el-form-item class="btns"> <el-button type="primary" @click="Login">登录</el-button> <el-button type="info" @click="resetLoginForm">重置</el-button> </el-form-item> <div style="display:flex; justify-content:flex-start">{{loginForm.message}}</div> </el-form> </div> </div> </div> </template> <style lang="less" scoped> .login_container { background-color: darkcyan; height: 100%; } .login_box { width: 350px; height: 300px; background-color: white; border-radius: 15px; /*容器内居中*/ position: absolute; left: 40%; top: 50%; transform: translate(-50%, -50%); .avatar_box { height: 130px; width: 130px; border: 1px solid #eee; border-radius: 50%; padding: 10px; /*边框阴影*/ box-shadow: 0 0 10px #ddd; position: absolute; left: 50%; transform: translate(-50%, -50%); background-color: #fff; img { width: 100%; height: 100%; border-radius: 50%; background-color: #993d3d; } } .login_form { position: absolute; bottom: 0; width: 100%; padding: 0 20px; box-sizing: border-box; } .btns { display: flex; justify-content: flex-end; } .wai { background-image: url("../assets/back.jpg"); width: 577px; height: 300px; border-radius: 15px; } } </style> <script> import qs from 'qs' export default { name: 'LoginPage', data () { return { // 数据绑定对象 loginForm: { username: 'lili', password: '123', message: '' }, loginFormRules: { // 验证用户 username: [ {required: true, message: '请输入用户名', trigger: 'blur'}, { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' } ], password: [ {required: true, message: '请输入登录密码', trigger: 'blur'}, { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' } ] } } }, methods: { Login () { /* http://localhost:8081/api/login */ this.$axios .post('http://localhost:8081/api/login', qs.stringify(this.loginForm)) .then(successResponse => { console.log(successResponse.data) if (successResponse.data.code !== 200) { this.loginForm.message = successResponse.data.message } else { console.log('登录成功') console.log(successResponse.data.data.user.user_account) this.$router.push({path: '/JumpTest', query: {param: successResponse.data.data.user.user_account}}) } }) .catch(failResponse => { console.log('12345566') console.log(failResponse) }) }, // // 重置登录表单 resetLoginForm () { this.$refs.loginFormRef.resetFields() } } } </script>
第二步:修改routes.js,配置props
import LoginPage from '@/views/LoginPage.vue' import JumpTest from '@/views/JumpTest.vue' import HomeTest from '@/views/HomeTest.vue' import AboutTest from "@/views/AboutTest.vue"; const routes = [ { name: 'LoginPage', path: '/', component: LoginPage }, { name: '/JumpTest', path: '/JumpTest', redirect: '/HomeTest', component: JumpTest, children: [ {path: '/HomeTest', name: 'HomeTest', component: HomeTest}, {path: '/AboutTest', name: 'AboutTest', component: AboutTest} ], props: route => ({param: route.query.param}) } ]; export default routes
第三步:修改HeaderTest.vue,运用传递的参数
<script> export default { name: 'HeaderPage', data() { return { user_name: this.$route.query.param } } } </script> <template> <div> <div class="head"> <div> <span>{{user_name}}</span> </div> </div> <div class="line" /> </div> </template> <style scoped> .head { display: flex; align-items: center; justify-content: right; height: 50px; background-color: #222832; } .head > div { display: flex; align-items: center; margin-right: 20px; } .head span { font-size: 15px; color: #ffffff; } .line { border-top: 1px solid hsla(0,0%,100%,.05); border-bottom: 1px solid rgba(0,0,0,.2); } </style>
第四步:运行程序npm run serve
可以看到lili已经成功显示了。