react中多个路由的配置

首先使用react路由,肯定要先安装
安装命令为:

npm install react-router-dom -S  

我们平常都是直接创建一个reactRouter.js文件,直接在里面写上如下代码

import React from "react";
import Home from "./component/Home";
import New from "./component/New";
import About from "./component/About";//引入路由文件
import { BrowserRouter, HashRouter, Route, Link} from "react-router-dom";
class reactRouter extends React.Component {
  render() {
    return (
    //根容器  HashRouter写在根容器中,只写一次就ok
      <HashRouter>
        <div>
          <h1>这是根目录</h1>
          <hr />
          <Link to="/home">邓紫棋偶像</Link>&nbsp;&nbsp;
          <Link to="/new">熊梓淇男神</Link>&nbsp;&nbsp;
          <Link to="/about">袁姗姗女神</Link>//在Link内置组件中,配置to属性,进行跳转:
        </div>
        //引入路由文件 进行跳转
        <Route path="/home" component={Home}></Route>//在Route内置组件中,配置path规则:
        <hr />
        <Route path="/new"  component={New}></Route>
        <hr />
        <Route path="/about" component={About}></Route>
      </HashRouter>
    );
  }
}
export default reactRouter;//向外暴露

这种写法一般只适用于初学者,而且需要跳转的页面比较多的话,这样也不方便管理。
现在写写配置多个路由的代码,欢迎大家提出自己不同的想法,去更好的改进
1.首先创建一个router文件夹,在里面创建Config.js和Router.js 两个人文件
Config.js中导入代码:

import React from 'react';
let config = [   {
  name: 'xxxxxx',
  path: '/xxxxxx',
  exact: true,
  component:  () => import('../pages/Gem')
},  {
  name: 'xxxxxx',
  path: '/xxxxxx',
  exact: true,
  component:  () => import('../pages/Travel')
},
...
]
export default config

Router.js导入代码:

import React, { Component } from 'react'
import Config from './Config'
import NotFound from '../pages/404'
import { BrowserRouter, Switch, Route } from 'react-router-dom'
export default class Router extends Component {
  render() {
    return (
      <BrowserRouter>
        <Switch>
          {/* 导入相关路由配置 */}
          {
            Config.map((item, index) =>   {return <Route exact={item.exact} key={index} path={item.path} component={item.component} />})
          }
          <Route component={NotFound} />
        </Switch>
      </BrowserRouter>
    )
  }
}

注:React Router 里的路径匹配以及组件加载都是异步完成的,不仅允许你延迟加载组件,并且可以延迟加载路由配置。在首次加载包中你只需要有一个路径定义,路由会自动解析剩下的路径。
这样方便配置多个路由,并且也是开发项目中最常用的路由配置,若有不同看法,欢迎指出恩

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
如果您想在 Vue 实现多选后跳转多个路由,可以按照以下步骤进行操作: 1. 首先,确定您使用的是 Vue Router 进行路由管理。如果没有安装 Vue Router,可以通过以下命令进行安装: ``` npm install vue-router ``` 2. 在您的 Vue 项目创建一个路由文件,比如 `router.js`。在该文件,导入 Vue 和 Vue Router,并使用 `Vue.use(VueRouter)` 来启用路由功能。 ```javascript import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) ``` 3. 在 `router.js` 定义您的路由配置,包括多个路由和对应的组件。 ```javascript import Home from './components/Home.vue' import Page1 from './components/Page1.vue' import Page2 from './components/Page2.vue' const routes = [ { path: '/', component: Home }, { path: '/page1', component: Page1 }, { path: '/page2', component: Page2 } ] const router = new VueRouter({ routes }) ``` 4. 在您的 Vue 组件,使用 `<router-link>` 组件来实现页面跳转,并通过 `to` 属性指定目标路由的路径。 ```html <template> <div> <ul> <li> <router-link to="/">Home</router-link> </li> <li> <router-link to="/page1">Page 1</router-link> </li> <li> <router-link to="/page2">Page 2</router-link> </li> </ul> </div> </template> ``` 5. 如果您想在多选后跳转多个路由,可以在组件处理多选的逻辑,并在适当的时候使用 `this.$router.push()` 方法来实现路由跳转。 ```javascript methods: { handleMultiSelect() { // 假设选路由路径存储在 selectedRoutes 数组 const selectedRoutes = ['/page1', '/page2'] // 遍历选路由路径,并逐个进行路由跳转 selectedRoutes.forEach(route => { this.$router.push(route) }) } } ``` 通过以上步骤,您可以实现在 Vue 多选后跳转多个路由的功能。请根据您的具体需求进行相应的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值