Vue的路由嵌套和参数传递(单个和多个)

Vue的路由嵌套

路由嵌套能够实现主页面点击跳转之后子页面展示到相应的位置

如这里的一个简单路由嵌套实现页面跳转的示例
点击用户信息即跳转到用户信息页面
在这里插入图片描述
点击用户列表即跳转到用户列表页面
在这里插入图片描述

  • 项目前期准备
  1. 使用vue-cli脚手架搭建好一个空项目
  2. 安装好路由组件vue-router
  3. 安装好element-ui
  • 我的项目结构
    在这里插入图片描述

  • 使用element-ui的导航组件,构建主页面即 首页Home.vue
    router-link是跳转的地址,路由地址需要在路由的js中进行配置
    router-view页面显示的位置

    <template>
    <div>
      <el-row>
        <el-col :span="6">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
            <el-submenu index="1">
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span>User</span>
              </template>
    			<!-- router-link是跳转的地址,需要在路由中进行配置 -->
                <router-link to="/UserInfo" tag="span">
                  <el-menu-item index="1-1">用户信息</el-menu-item>
                </router-link>
                <router-link to="/UserList" tag="span">
                  <el-menu-item index="1-2">用户列表</el-menu-item>
                </router-link>
            </el-submenu>
            <el-menu-item index="2">
              <i class="el-icon-menu"></i>
              <span slot="title">导航二</span>
            </el-menu-item>
          </el-menu>
        </el-col>
        <el-col :span="18">
          <!-- router-view页面显示的位置 -->
          <router-view></router-view>
        </el-col>
      </el-row>
    </div>
    </template>
    <script>
    export default {
      methods: {
        handleOpen (key, keyPath) {
          console.log(key, keyPath)
        },
        handleClose (key, keyPath) {
          console.log(key, keyPath)
        }
      }
    }
    </script>
    
    <style>
    </style>
    
    
  • 创建好两个子页面

    • 我的UserInfo.vue页面

      <template>
        <h2>用户信息</h2>
      </template>
      
      <script>
      export default {
        name: 'UserInfo'
      }
      </script>
      
      <style scoped>
      
      </style>
      
      
    • 我的UserList.vue页面

      <template>
        <h2>用户列表</h2>
      </template>
      
      <script>
      export default {
        name: 'UserInfo'
      }
      </script>
      
      <style scoped>
      
      </style>
      
      
  • 完成页面创建之后,在路由中进行配置路由
    导入刚刚创建的三个页面
    将HOME作为主页面
    children里面就是它的子页面,子路由的配置方法和结构与主页面相同

    import Vue from 'vue'
    import Router from 'vue-router'
    import Home from '@/components/Home'	// 导入刚刚创建的三个页面
    import UserInfo from '@/components/User/UserInfo'
    import UserList from '@/components/User/UserList'
    
    Vue.use(Router)
    
    export default new Router({
      routes: [
        {
          path: '/',
          name: 'home',
          component: Home,	// 将HOME作为主页面
          children: [	// children里面就是它的子页面,子路由的配置方法和结构与主页面相同
            {
              path: '/UserInfo',
              component: UserInfo
            },
            {
              path: '/UserList',
              component: UserList
            }
          ]
        }
      ]
    })
    
    

    Vue的单个参数传递

    在以上页面的基础上,实现传参

  • 传递参数的方式,在路由跳转的时候加上params对象

    <router-link :to="{name: 'UserInfo', params: {id:2}}" tag="span">
        <el-menu-item index="1-1">用户信息</el-menu-item>
    </router-link>
    
  • 改造路由配置

    在路由地址后面通过/:参数名的方式,添加要接收的参数名,注意必须和传递的参数名保持一致

    routes: [
        {
            path: '/',
            name: 'home',
            component: Home,
            children: [
                {
                    path: '/UserInfo/:id',	// 携带参数的路由路径配置方式
                    name: 'UserInfo',
                    component: UserInfo
                },
                {
                    path: '/UserList',
                    name: 'UserList',
                    component: UserList
                }
            ]
        }
    ]
    
  • 接收参数并使用

    通过$route.params对象来接收,然后进行使用

    <template>
    <div>
      <h2>用户信息</h2>
      <!-- 通过$route.params对象来接收 --> 
      <h3>收到的参数:{{$route.params.id}}</h3>
    </div>
    </template>
    
    <script>
    export default {
      name: 'UserInfo'
    }
    </script>
    
    <style scoped>
    
    </style>
    
    
  • 实现的效果

    这里的路由地址后面会跟上我们传递的参数

    我们也能看到 页面收到的参数值

在这里插入图片描述

Vue的多参数传递

通过props传递多个参数的实现方式(在以上的基础上进行完善和改造)

  • 路由跳转携带参数的方式

    <router-link :to="{name: 'UserInfo', params: {id:2, name: 'xiaomin'}}" tag="span">
        <el-menu-item index="1-1">用户信息</el-menu-item>
    </router-link>
    
  • 路由配置规则的修改

    添加props为true

      routes: [
        {
          path: '/',
          name: 'home',
          component: Home,
          children: [
            {
              path: '/UserInfo',
              name: 'UserInfo',
              component: UserInfo,
              props: true	//添加props为true
            },
            {
              path: '/UserList',
              name: 'UserList',
              component: UserList
            }
          ]
        }
      ]
    
  • 参数的接受和使用方式

    通过props放hi接收参数,然后直接使用

    <template>
    <div>
      <h2>用户信息</h2>
      <h3>收到的参数:ID:{{id}}</h3>	<!-- 直接进行使用 -->
      <h3>收到的参数:Name:{{name}}</h3>
    </div>
    </template>
    
    <script>
    export default {
      props: ['id', 'name'],	// 通过props放hi接收参数
      name: 'UserInfo'
    }
    </script>
    
    <style scoped>
    
    </style>
    
    
  • 实现的效果,通过props的方式传递的话参数不会直接携带到URL里面
    在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Vue3中,你可以通过以下方式同时传递多个参数: 1. 在路由配置中定义参数: ``` const routes = [ { path: '/example', name: 'example', component: ExampleComponent, props: { foo: 'foo', bar: 'bar' } } ] ``` 2. 使用动态路由: ``` <router-link :to="{ name: 'example', params: { foo: 'foo', bar: 'bar' } }">Go to Example</router-link> ``` 3. 使用查询参数: ``` <router-link :to="{ name: 'example', query: { foo: 'foo', bar: 'bar' } }">Go to Example</router-link> ``` 无论哪种方式,你都可以在目标组件中通过 `props` 或 `$route` 来获取传递的参数。例如: ``` export default { props: ['foo', 'bar'], mounted() { console.log(this.foo, this.bar); console.log(this.$route.params.foo, this.$route.params.bar); console.log(this.$route.query.foo, this.$route.query.bar); } } ``` ### 回答2: 在Vue3中,我们可以通过使用路由传参的方式同时传递多个参数。下面是一个简单的示例。 首先,在定义路由时,我们需要设置路由参数。可以在路由的配置对象中使用`props`属性来设置参数。例如: ```javascript const routes = [ { path: '/details', name: 'details', component: DetailsComponent, props: true // 使用props来启用参数传递 } ] ``` 然后,在使用路由跳转时,我们可以通过在`$router.push`方法中传递一个对象来传递多个参数。例如: ```javascript this.$router.push({ name: 'details', params: { id: 123, name: 'Product A', price: 9.99 } }) ``` 最后,在接收参数的组件中,可以通过`props`属性来声明接收的参数,并直接在组件中使用。例如: ```javascript export default { props: { id: { type: Number, required: true }, name: { type: String, required: true }, price: { type: Number, required: true } }, // ... } ``` 这样,我们就可以同时传递多个参数,并在目标组件中接收并使用这些参数了。 ### 回答3: 在Vue3中,可以使用路由params参数和query参数来传递多个参数。 1. 使用params参数传递多个参数: ```javascript // 发送路由请求时传递多个参数 this.$router.push({ path: '/example', params: { param1: value1, param2: value2 } }); // 在目标路由组件中接收参数 value1 = this.$route.params.param1; value2 = this.$route.params.param2; ``` 2. 使用query参数传递多个参数: ```javascript // 发送路由请求时传递多个参数 this.$router.push({ path: '/example', query: { param1: value1, param2: value2 } }); // 在目标路由组件中接收参数 value1 = this.$route.query.param1; value2 = this.$route.query.param2; ``` 注意:params参数传递参数会显示在URL中,例如:`/example/param1/param2`。而query参数传递参数会在URL之后添加`?`,例如:`/example?param1=value1&param2=value2`。根据具体需求来选择适合的传参方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值