programing

대응 라우터 URL 매개 변수가 작동하지 않음

prostudy 2022. 4. 1. 18:24
반응형

대응 라우터 URL 매개 변수가 작동하지 않음

Ract Router의 URL 매개 변수 기능을 사용할 수 없으므로 통찰력이 유용할 수 있음!나는 'react-router' 대신 'react-router-dom-dom'을 사용하려고 했지만, 이 남자가 받는 오류는 다음과 같다: https://github.com/ReactTraining/react-router/issues/1799

localhost:8000을 하면 앱이 작동한다.localhost:8000/123으로 이동하면 브라우저에서 index.html무반응으로 렌더링한다.익스프레스가 리액션을 방해하는가?익스프레스에서 내가 가진 전부는 다음과 같다.

app.get('*', function response(req, res) {
  res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
  res.end();
});

여기메인 요원이 있다.

import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import Routes from './shared/components/Routes';
const ROOT_ELEMENT = 'app';
ReactDOM.render((
   <Router history={browserHistory}>
   {Routes}
   </Router>
), document.getElementById(ROOT_ELEMENT));

가 가는 길.js:

import { Route, IndexRoute, Link } from 'react-router';

import App from './App';
import HomePage from '../../pages/home/page';
import AboutPage from '../../pages/about/page';

const Child = ({ match }) => (
  <div>
    <h3>ID: {match.params.id}</h3>
  </div>
)

export default (
  <Route path="/" component={App}>
    <IndexRoute component={HomePage} />
    {/* <Route path="about" component={AboutPage} /> */}
    <Route path="/:id" component={Child} />
  </Route>
);

구성 요소는 다른 파일에서 다음과 같이 정의된다(모든 가져오기 및 라이프사이클 기능은 제외).

export default React.createClass({
  render: function() {
     {* bunch of JSX goes here *}
  }
});

요청한 대로 index.html 파일:

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>Consent Form</title>
  <meta name="description" content="Privacy Consent Webform">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--Google Material Design Icons-->
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
  <div id="app"></div>
</body>
</html>

내 웹 팩 구성(다발이 주입됨!) 때문에 html에 스크립트 태그로 번들이 없는 경우(다음 코드는 구성의 일부일 뿐이다).

entry: [
    'webpack-hot-middleware/client',
    'bootstrap-loader',
    path.join(__dirname, 'src/main.js')
  ],
  output: {
    path: path.join(__dirname, '/dist/'),
    filename: '[name].js',
    publicPath: '/'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.tpl.html',
      inject: 'body',
      filename: 'index.html'
    })

당신이 당신의 앱에 새로운 루트를 추가할 때, 당신은 새로운 라우트+ 컴포넌트를 주입하기 위해 핫 재로드에 의존할 수 없다.Ctrl+C를 누르고 실행해야 했다.npm start다시 한 번

이렇게 하면 문제가 해결된다!!

또한 react-Router-dom은 react-Router v4 패키지인 반면 react-Router는 이전 버전이다.그 점을 지적해 준 @HalCarleton 덕분이다.공식 문서에서는 리액터-루터-도메인을 보여주는 동안 나는 모든 곳에서 리액터-루터-루터 코드 사례를 보았다. 그리고 나는 왜 두 가지 다른 종류의 패키지가 있는지 정말 혼란스러웠다.

이런 일을 하도록 해봐라.

<Switch>
      <Route path="/auth/login/:token" render={props => <Login  {...this.props} {...props}/>}/>
      <Route path="/auth/login" component={Login}/>
   </Switch>

먼저 매개 변수가 있는 경로와 매개 변수가 없는 경로 후 경로.내 로그인 구성 요소 안에 이것을 넣는다.console.log(props.match.params.token);날 위해 시험해보고 일해줬어

참조URL: https://stackoverflow.com/questions/43739338/react-router-url-parameters-not-working

반응형