简体   繁体   English

在应用程序之外调用路由更改(react-router)

[英]Calling route changes outside app (react-router)

I'm building a react app, and i need to change a component when the URL changes.我正在构建一个 React 应用程序,当 URL 更改时,我需要更改一个组件。

Here's my app.js:这是我的 app.js:

ReactDOM.render(
    <Provider store={Store}>
        <BrowserRouter>
            <App />
        </BrowserRouter>
    </Provider>
, document.getElementById('root'));

Main.js主程序

export default class Main extends Component {
    render() {
        return(
            <div className="d-flex flex-column col-md-12 col-lg-10 p-0 bg-f8f6f9">
                <div className="box-header">
                    <div className="container-edit">
                        <MainMenu/>
                    </div>
                </div>
                <Pagina/>
            </div>
        );
    }
}

MainMenu.js主菜单.js

...
<Router>
    ...
    <Link className="dropdown-item" to="/administrativo/cadastro/grupos">Grupos</Link>
    ...
</Router>
...

Pagina.js (the component that is responsible for load the specific page) Pagina.js(负责加载特定页面的组件)

export default class Pagina extends Component {
    render() {
        return(
            <div id="page">
                <Router>
                    <Switch>
                        <Route path="/administrativo/cadastro/grupos" component={AdministrativoGrupos} />
                    </Switch>
                </Router>
            </div>
        );
    }
}

The "component" that i'm trying to load, is AdministrativoGrupos , but when i click on the <Link> to this url, nothing happens.我试图加载的“组件”是AdministrativoGrupos ,但是当我点击这个网址的<Link> ,什么也没有发生。

I already tried the exact on <Link> , but nothing changes.我已经尝试过exact<Link> ,但没有任何变化。

A interesting fact: if i click on the <Link> , nothing happens.一个有趣的事实:如果我点击<Link> ,什么都不会发生。 After that, if i click on a <a href="#"> , the component loads normally.之后,如果我点击<a href="#"> ,组件会正常加载。

Any suggestions?有什么建议?

Edit: AdministrativoGrupos import React, { Component } from 'react';编辑:AdministrativoGrupos import React, { Component } from 'react';

export default class AdministrativoGrupos extends Component {
    render() {
        return(
            <div className="w-80 ml-auto mr-auto mt-4">
                <div className="row">
                    <div className="col-lg-6">
                        <nav className="Breadcrumb">
                            <ol>
                                <li>Administrativo <i className="fa fa-chevron-right" aria-hidden="true"></i></li>
                                <li>Cadastro <i className="fa fa-chevron-right" aria-hidden="true"></i></li>
                                <li>Grupos</li>
                            </ol>
                        </nav>
                    </div>
                    <div className="col-lg-6">
                        <div className="d-flex box-pesquisar-grupos">
                            <div className="box-pesquisar-grupos-input">
                                <select v-model="selected" id="group-select" className="js-example-responsive" disabled>
                                </select>
                            </div>
                            <i className="fa fa-file-o" aria-hidden="true"></i>
                        </div>
                    </div>
                </div>
                <div className="row">
                    <div className="content-grupos">
                        <p className="p-style1">Grupo*</p>

                        <button className="btn btn-info add-grupo-js">Adicionar Grupo</button>
                        <button className="btn btn-danger delete-grupo-js">Deletar
                            Grupo</button>

                        <div className="float-left w-100 table-wrapper box-table-grupos js-scrollbar2">
                            <form id="form-group" method="POST">
                                <table className="table table-grupos">
                                    <thead>
                                        <tr>
                                            <th scope="col">Item do menu</th>
                                            <th scope="col">Incluir</th>
                                            <th scope="col">Excluir</th>
                                            <th scope="col">Alterar</th>
                                            <th scope="col">Pesquisar</th>
                                            <th scope="col">Visualizar</th>
                                            <th scope="col">Todos</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr id="permission1e">
                                            <th scope="col">
                                                Teste
                                                <input type="hidden" className="id-js" name="permissoes[1e][id]"
                                                    value="1e"/>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="insert check-option-js" name="permissoes[1e][insert]"
                                                        type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="delete check-option-js" name="permissoes[1e][delete]"
                                                        type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="update check-option-js" name="permissoes[1e][update]"
                                                        type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="search check-option-js" name="permissoes[1e][search]"
                                                        type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="read check-option-js" name="permissoes[1e][read]"
                                                        type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                            <th scope="col">
                                                <label className="persona-check-e-radio">
                                                    <input className="all check-option-js" type="checkbox"/>
                                                    <span className="checkmark"></span>
                                                </label>
                                            </th>
                                        </tr>
                                    </tbody>
                                </table>
                                <button className="btn btn-info">Salvar</button>
                                <input type="hidden" name="groupid" id="groupid"/>
                                <input type="hidden" name="isdefault" id="isdefault"/>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

I couldn't test your partial snippets of code, but I will point out some things which I think might be causing some problems:我无法测试您的部分代码片段,但我会指出一些我认为可能会导致一些问题的事情:

  1. You cannot use exact on Link .您不能在Link上使用exact exact prop is in Route component. exact道具在Route组件中。
  2. You don't need to surround Link component around Router .您不需要在Router周围环绕Link组件。 Try again without that.没有那个再试一次。

Also is your url at least changing after clicking on Link ?点击Link后,您的网址是否至少会发生变化?

Let me know if this helps you.如果这对您有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM