请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

5-9 多级路由跳转不正确

问题描述:我在Routes.js里把 '/'路由的exact属性注释后,感觉所以 / 一下的都会走Home,/ttt的Login则不会渲染,而且点击Login的Link后,也会把Home一起渲染。

我检测了client和server端的代码,都没找到问题,而且我也对比了老师5-8,5-9,5-10的代码也没发现什么,视频也重复看了几遍,还是不行。请老师帮忙解答一下
client/index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, Route } from 'react-router-dom'
import routes from '../Routes'
import { Provider } from 'react-redux'
import getStore from '../store'

const App = () => {
  return (
    <Provider store={getStore()}>
      <BrowserRouter>
        <div>
          {routes.map((route) => <Route {...route} />)}
        </div>
      </BrowserRouter>
    </Provider>
  )
}
ReactDOM.hydrate(<App />, document.getElementById('root'));

server/index.js

import express from 'express'
import { render} from './util'

const app = express();
app.use(express.static('public'))

app.get('*', function (req, res) {
  
  res.send(render(req));
});

const server = app.listen(3000);

server/util.js

import React from 'react'
import { renderToString } from 'react-dom/server'
import { StaticRouter, Route, matchPath } from 'react-router-dom'
// import { matchRoutes } from 'react-router-config'
import routes from '../Routes'
import { Provider } from 'react-redux'
import getStore from '../store'

export const render = (req) => {
  const store = getStore()

  // const matchedRoutes = matchRoutes(routes, req.path)
  const matchRoutes = []
  // 根据路由的路径,来往store里面加数据
	routes.some(route => {
	  const match = matchPath(req.path, route)
	  if (match) {
	  	matchRoutes.push(route)
    }
  })

  console.log(matchRoutes)

  const content = renderToString(
    <Provider store={store}>
      <StaticRouter context={{}} location={req.path}>
        <div>
        // 这里的matchRoutes,我也改成routes,效果一样
          {matchRoutes.map((route) => (<Route {...route} />))}
        </div>
      </StaticRouter>
    </Provider>
  );

  return (
    `
      <html>
        <head>
          <title>ss</title>
        </head>
        <body>
          <div id="root">${content}</div>
          <script src='/index.js'></script>
        </body>
      </html>
    `
  )
}

Routes.js

import Home from './containers/Home'
import Login from './containers/Login'

export default [
  {
    path: '/',
    component: Home,
    // exact: true,
    loadData: Home.loadData,
    key: 'home',
    routes: [
      {
        path: '/ttt',
        component: Login,
        exact: true,
        key: 'ttt',
      },
    ],
  },
  {
    path: '/login',
    component: Login,
    exact: true,
    key: 'login',
  },
]

package.json

"dependencies": {
    "@babel/preset-react": "^7.0.0",
    "axios": "^0.18.0",
    "babel-preset-stage-0": "^6.24.1",
    "express": "^4.16.4",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router-config": "^4.4.0-beta.6",
    "react-router-dom": "^4.3.1",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "webpack-merge": "^4.1.5",
    "webpack-node-externals": "^1.7.2"
  },
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "babel-loader": "^8.0.4",
    "webpack": "^4.28.3",
    "webpack-cli": "^3.1.2"
  }

正在回答 回答被采纳积分+3

5回答

八神光 2019-01-15 20:38:58

Routes.js有问题,改成这样

export default [
  {
    path: '/',
    component: Home,
    exact: true,
    key: 'home'
  },
  {
    path: '/ttt',
    component: Login,
    exact: true,
    key: 'ttt'
  },
  {
    path: '/login',
    component: Login,
    exact: true,
    key: 'login',
  },
]

原先的代码里写的是二级路由,只要路径中包含/就会加载home组件,加载/路由的所有下级路由,home组件都会加载。如果不想这样的话,直接改成一级路由平铺开写就行。

建议去看看react-router的文档,真正明白路由的原理就容易自己找到问题了~

1 回复 有任何疑惑可以回复我~
Arya_Stark 2019-08-04 00:36:49

react-router 4 中好像把嵌套路由的规则重写了,router 本身就是一个组件, 不在支持这种配置式的嵌套路由写法。 不知道理解的对不对

0 回复 有任何疑惑可以回复我~
qq_我的心向大海_ckuGNU 2019-05-20 12:20:10

的确,是不会渲染的,因为我们在子组件中的父组件中没有使用

import { renderRoutes } from "react-router-config";的方法,并且要在client下面的index.js文件下面将

{renderRoutes(routes)}替换

    {routes.map(route => (

                <Route {...route}/>

                ))}

            </div>

看这个连接里面的最后一个例子:https://github.com/reacttraining/react-router/tree/master/packages/react-router-config

0 回复 有任何疑惑可以回复我~
Dell 2019-01-05 22:24:28

同学你好,我一下子也看不出这么多代码中具体的问题,我建议采用beyondCompare这个工具,仔细比对我们的代码差异,然后修改即可。当然,你要先看下,我的代码是否可以正确运行

0 回复 有任何疑惑可以回复我~
提问者 慕运维801387 2019-01-05 18:41:23

http     https://git.imooc.com/happy/react_server_demo.git

ssh      ssh://git@git.imooc.com:80/happy/react_server_demo.git

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信