vue3.0-router的子路由创建,路由的传值操作,单页面多路由操作

本文是对jspang-vue-router学习的个人总结

1.子路由的创建

首先我们创建三个组件,分别是hi父组件,hi1子组件1,hi2子组件二

<template>
  <h2>我是大帅比{{msg}}</h2>
  
</template>

<script>
export default {
    name:"hi",
    data(){
        return {
            msg: "i am hi page"
        }
    }
}
</script>

<style>

</style>
<template>
  <h2>{{msg}}</h2>
</template>
<script>
export default {
    name:"hi1",
    data(){
        return {
            msg: "i am hi1111 page"
        }
    }
}
</script>

<style>

</style>
<template>
  <h2>{{msg}}</h2>
</template>

<script>
export default {
    name:"hi2",
    data(){
        return {
            msg: "i am hi2222 page"
        }
    }
}
</script>

<style>

</style>

子路由是要实现什么效果呢,就是一个页面可以显示同的界面,每个不同的界面就写在子路由里面,例如hi1页面就可以出现hi1的界面,也可以出现hi2的界面。

为了能够显示不同的子路由界面(子界面),我们需要给hi添加一个标签

<template>
  <h2>我是大帅比{{msg}}</h2>
  <router-view> </router-view>
  
</template>

<script>
export default {
    name:"hi",
    data(){
        return {
            msg: "i am hi page"
        }
    }
}
</script>

<style>

</style>

router-view标签搭配router-link使用,它的具体作用是:router-view 当你的路由path 与访问的地址相符时,会将指定的组件替换该 router-view -->

接下来我们需要再router->index.js中设置子路由,这也是设置子路由的关键:

import { createRouter, createWebHashHistory } from 'vue-router'
import hi from '@/components/hi.vue'
import hi1 from '@/components/hi1.vue'
import hi2 from '@/components/hi2.vue'

const routes = [
  {
    path:'/hi',
    name:'hi',
    component:hi,
    children: [
      {
      path:'/',
      name:"hi",
      component:hi,
    },
    {
      path:'/hi1',
      name:"hi1",
      component:hi1,
    },
    {
      path:'/hi2',
      name:"hi2",
      component:hi2,
    },
  ]
  }//有子路由的话 name要设置在children里
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

  1. 首先需要引入组件

    import hi from '@/components/hi.vue'
    import hi1 from '@/components/hi1.vue'
    import hi2 from '@/components/hi2.vue'
    

2.绑定子-父关系:父界面是hi,所以

{
    path:'/hi',
    name:'hi',
    component:hi,
  }

子界面是hi1和hi2,用children数组表示

children: [
    {
      path:'/hi1',
      name:"hi1",
      component:hi1,
    },
    {
      path:'/hi2',
      name:"hi2",
      component:hi2,
    },
  ]

3.接下来再APP.vue中添加路径就好了

<template>
  <nav>
    <router-link to="/hi">hiPage</router-link>|
    <router-link to="/hi1">hiPage1</router-link>|
    <router-link to="/hi2">hiPage2</router-link>|
    <router-view></router-view>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>

这里的是显示第一层的view,和hi中的view不太一样,hi中的是显示子路由的view

结果显示:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.如何用路由传递和接受参数

可以用router中的name传值,但是这样传值只能传一个name,我们只介绍最常用的方法,利用router-link传递参数

传递操作:

这里拿hi1举例,在App.vue文件中

<router-link 
:to="{name:'h1',params:{username:'我是h1',id:777}}">
hiPage1
</router-link>

和之前我们写的router-link不同

<router-link to="/hi1">hiPage2</router-link>|

router-link标签类似与a标签,to后面的就相当于url,只不过我们在router-index.js中设置过,如果想用router-link传递参数,首先我们要做的就是把组件的路径绑定,不同于用to=“/hi1"绑定,而是:to=”{name:‘h1’}这个name的值一定要和router中给hi1的name一致

也就是

{
      path:'/hi1',
      name:"hi1",
      component:hi1,
    },

这样我们就完成了最基本的绑定操作,然后传值操作,就利用params:{username:‘我是h1’,id:777}这种对象的格式来传递就好了

取值操作:

我们在hi1组件里进行取值操作

<template>
  <h2>{{msg}}--{{$route.params.username}}--{{$route.params.id}}</h2>
  <h2>{{msg}}</h2>
</template>
<script>
export default {
    name:"hi1",
    data(){
        return {
            msg: "i am hi1111 page"
        }
    }
}
</script>

<style>

</style>

利用$router.params.xxx来进行取值

在这里插入图片描述

3.单页面多路由操作

单页面多路由操作,也就是一个页面可以分成不同的部分,每个不同的部分就是一个路由,也就是一个组件。它和子路由的区别就在于子路由一直是单页面单路由,也就是说一次只能显示一个子路由。

也是用到components中的hi,hi1,hi2这三个组件,和第一节一样

单页面多路由,我们要实现的效果就是在hi页面中给分成两部分,左边部分是hi1,右边是hi2,这里要注意虽然hi1和hi2是在hi中显示的但是并没有父子关系,所以我们不能把router-view设置在hi中

要实现多路由操作需要:

在路由中设置:注意设置多路由components一定要加s

import { createRouter, createWebHashHistory } from 'vue-router'
import hi from '@/components/hi.vue'
import hi1 from '@/components/hi1.vue'
import hi2 from '@/components/hi2.vue'

const routes = [
  {
    path:'/hi',
    name:'hi',
    components:{
      default:hi,
      top:hi1,
      bottom:hi2
    }  
  }
  
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

在App.vue中添加,多路由的特点,有多个router-view标签

<template>
  <nav>
    <router-link to="/hi">hiPage</router-link>|
    <router-view></router-view>
    </nav>
  <router-view name="top" 									style="width:500px;height:200px;background-			color:#1212;margin:0 auto;"></router-view>
  <router-view name="bottom" 								style="width:500px;height:200px;background-			color:#1212;margin:0 auto;">
  </router-view> 
</template>

给router-view添加name属性,这个属性要和router-index.js中components中设置的一样,以此完成路由和显示效果(router-view)的绑定

效果如下:

在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Vue3中,使用路由需要在父组件中添加一个`<router-view>`标签,并将组件的路由配置添加到父组件的路由配置中。 以下是一个示例代码: ```javascript import { createRouter, createWebHistory } from 'vue-router' import ParentComponent from './components/ParentComponent.vue' import ChildComponent from './components/ChildComponent.vue' const routes = [ { path: '/parent', name: 'ParentComponent', component: ParentComponent, children: [ { path: 'child', name: 'ChildComponent', component: ChildComponent } ] } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 在上面的代码中,我们首先导入了`createRouter`和`createWebHistory`函数。然后,我们定义了两个组件`ParentComponent`和`ChildComponent`。接下来,我们定义了一个路由数组`routes`,其中包含一个名为`ParentComponent`的父路由和一个名为`ChildComponent`的路由路由通过在`children`数组中添加一个对象来定义。 最后,我们创建了一个`router`实例,并将其导出。我们可以将此路由实例用作Vue应用程序的路由器。在父组件中,我们可以使用`<router-link>`标签来链接到组件的路由。 示例父组件`ParentComponent`的代码: ```html <template> <div> <h2>Parent Component</h2> <router-link to="/parent/child">Go to Child Component</router-link> <router-view></router-view> </div> </template> ``` 在上面的代码中,我们使用`<router-link>`标签将用户导航到组件的路由。我们还添加了一个`<router-view>`标签,它将根据当前的路由渲染组件。 ### 回答2: 在Vue3中,路由的使用方法如下: 首先,我们需要在创建Vue应用程序的地方,使用`createRouter`函数创建一个路由实例。在创建路由实例时,我们可以配置路由的根路径、路由等相关信息。 ```javascript import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', component: Home, children: [ { path: 'page1', component: Page1 }, { path: 'page2', component: Page2 } ] } ] }) ``` 以上示例中,我们配置了一个路由根路径为`'/'`,并定义了两个路由`'page1'`和`'page2'`。当用户进入`'/'`路径时,会渲染`Home`组件,并根据当前路径(即路由路径)加载相应的组件`Page1`和`Page2`。 在使用路由的组件中,我们需要使用`<router-view>`标签来渲染路由对应的组件。在父组件的模板中插入`<router-view></router-view>`标签后,Vue会自动根据当前路径渲染匹配的组件。 ```javascript // Home.vue <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 注意,在Vue3中,我们需要使用`<router-view></router-view>`标签来渲染路由,而不是Vue2中的`<router-view/>`。 要使用路由,我们还需要在父组件中定义一个路由出口,用于渲染组件。 ```javascript // Page1.vue <template> <div> <h2>Page 1</h2> <!-- 组件内容 --> </div> </template> ``` 这样,当用户访问`'/'`路径时,会渲染`Home`组件,并在`<router-view></router-view>`标签中根据当前路径加载相应的组件。当用户访问`'/page1'`路径时,会渲染`Page1`组件,并将其显示在`<router-view></router-view>`标签中。 总结起来,Vue3中使用路由的方法是: 1. 在路由实例的配置项`routes`中,通过`children`属性定义路由。 2. 在父组件模板中使用`<router-view></router-view>`标签渲染路由。 3. 在每个组件的模板中填入组件内容。 ### 回答3: Vue 3中的路由使用方法与Vue 2中的类似,你可以使用Vue Router来管理和配置路由。 首先,在Vue项目中安装Vue Router。然后,在主路由文件(通常是main.js)中导入VueVue Router创建一个新的Vue Router实例,并将其与Vue实例关联。 然后,你可以在路由配置文件中定义主路由路由。主路由用于加载包含路由的组件,而路由是主路由下的具体组件。你可以在主路由的`children`选项中定义路由。 例如: ```javascript import { createRouter, createWebHistory } from 'vue-router'; import Home from './components/Home.vue'; import About from './components/About.vue'; import User from './components/User.vue'; import UserProfile from './components/UserProfile.vue'; import UserPosts from './components/UserPosts.vue'; const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About }, { path: '/user/:id', name: 'User', component: User, children: [ { path: 'profile', name: 'UserProfile', component: UserProfile }, { path: 'posts', name: 'UserPosts', component: UserPosts } ] } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router; ``` 在上面的例中,`/user/:id`是一个带有动态参数的父路由,它加载一个名为User的组件,并在`children`选项中定义了两个路由:`/user/:id/profile`和`/user/:id/posts`。这两个路由分别加载UserProfile和UserPosts组件。 在父组件User.vue中,你可以使用`<router-view>`组件来渲染路由的内容,路由的组件将根据所匹配的路由进行加载和切换。 希望这个简的示例能够帮助你理解Vue 3中路由的使用方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值