【问题标题】:Calling route changes outside app (react-router)在应用程序外部调用路由更改(react-router)
【发布时间】:2019-03-30 04:01:12
【问题描述】:

我正在构建一个 react 应用,当 URL 发生变化时,我需要更改一个组件。

这是我的 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

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

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>
        );
    }
}

我要加载的“组件”是AdministrativoGrupos,但是当我点击此网址的&lt;Link&gt; 时,什么也没有发生。

我已经在&lt;Link&gt; 上尝试了exact,但没有任何变化。

一个有趣的事实:如果我点击&lt;Link&gt;,什么都不会发生。之后,如果我点击&lt;a href="#"&gt;,组件会正常加载。

有什么建议吗?

编辑:AdministrativoGrupos 从'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>
        );
    }
}

【问题讨论】:

  • 你能提供一个 AdministrativoGrupos 组件的 sn-p 吗?
  • 完成。我对 React 没有太多经验,也许那里有问题。
  • 看起来不错。你检查过这个github.com/ReactTraining/react-router/blob/master/packages/…吗?
  • 不,我认为这是我的问题。我正在使用redux,我不知道他对此有影响。谢谢!

标签: javascript reactjs routes react-router


【解决方案1】:

我无法测试您的部分 sn-ps 代码,但我会指出一些我认为可能导致一些问题的事情:

  1. 您不能在Link 上使用exact。 exact 道具在 Route 组件。
  2. 你不需要围绕Link组件 Router。不用那个再试一次。

点击Link后,您的网址是否至少会发生变化?

如果这对你有帮助,请告诉我。

【讨论】:

  • 对Link 的确切表示抱歉,我指的是Route。我删除了包含链接的路由器,但没有改变结果。 URL 正在更改,但组件未加载。感谢您的关注和帮助。
  • 你能回答我关于点击Link后url是否被更改的问题
  • 是的,点击链接后网址会发生变化。
猜你喜欢
  • 2020-01-22
  • 2020-04-26
  • 2018-09-11
  • 2018-06-09
  • 1970-01-01
  • 2020-08-01
  • 2016-09-05
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多