vue路由传值和路由跳转


前言

一、vue路由配置步骤

1.第一步: 导入vue.js和vue-router.js插件

 <script src='vue.js'></script>
<script src="vue-router.js"></script>
  1. 第二步: 设置路由跳转的标签
<nav style="text-align: center">
            <a href="#/login">登录</a> |
            <a href="#/register">注册</a> |
            <a href="#/main">主页</a> |
            <a href="#/">其他</a> |
            <a href="#/one/two1">二级路由</a>
            <a href="#/one/two2">二级路由</a>
        </nav>
  1. 第三步: 创建路由管理的组件
<script>
 	let mycom1 = Vue.component("loginCom", {
    template: "<h1>这是登录页信息 <router-view></router-view></h1>"})
    let mycom2 = Vue.component("registCom", {
    template: "<h1>这是注册页信息</h1>"})
    let mycom3 = Vue.component("mainCom", {
    template: "<h1>这是主页信息</h1>"})
</script>
  1. 第四步: 创建路由对象,配置路由信息
  const router = new VueRouter({
   
            routes: [{
    path: "/login", component: mycom1 },
                	{
    path: "/register", component: mycom2 },
                	{
    path: "/main", component: mycom3 },
                	{
    path: "/" , redirect: "/main"},
					{
    path: "/one", component: mycom1,  children: [
                    {
   path: 'two1', component: mycom2},
                    {
   path: 'two2', component: mycom3},
                ]}
            ]
        })
  1. 第五步: 把路由对象绑定到vue根组件上
 var vm = new Vue({
   
            el: '#myApp',
            router
        })
  1. 第六步: 设置路由出口, 路由控制的组件渲染的位置
<router-view></router-view>

1. 路由跳转时共有以下四种传值方式:

  1. url路径拼接传值, 把数据以键值对形式拼接到url上, 使用$route.query接收
  2. 动态路由传值, 使用友好型url设置数据结构传值, 使用 $route.params接收
  3. 路由对象传值, 使用对象跳转路由,对象中query字段传值, 使用$route.query接收
  4. 命名路由传值, 使用路由名跳转并使用params字段传值, 使用$route.params接收

1. url路径拼接拼接

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值