sorrycc / ama

Ask me anything!

Home Page:https://github.com/sorrycc/ama/issues?q=is%3Aissue+is%3Aclosed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dva 中的 subscriptions 是利用什么方法监听到页面第一次进入路由的情况?

951565664 opened this issue · comments

commented

我看了下源码。也跑了一下,发现有listen事件能够监听。。。但是我验证的结果和查询到的文档,表面listen事件只能监听到利用history.push() 来更改路由的方法,而且不能监听页面第一次进入路由和通过 等router4的方法跳转路由。求解答

commented
import React from 'react'
import { render } from 'react-dom'
import { Router } from 'react-router'
import { Route, Redirect, Switch } from 'react-router-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import reducer from './reducers'
import 'todomvc-app-css/index.css'
import Header from './containers/Header'
import MainSection from './containers/MainSection'
import Home from './containers/Home'
import Page from './containers/Page'
import { ConnectedRouter } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory'
import createHashHistory from 'history/createHashHistory'

const store = createStore(reducer)
// const history = syncHistoryWithStore(createHistory(),store)
const history = createHistory()
// const hashHistory = createHashHistory()


const unlisten = history.listen((location, action) => {
  // location is an object like window.location
  console.log('aaa', action, location.pathname, location.state)
})

// Use push, replace, and go to navigate around.
// history.push("/home", { some: "state" })

// To stop listening, call the function returned from listen().
unlisten()

const Todomvc = () => <div>
  <Header />
  <MainSection />
</div>

render(
  <Router history={history}>
    <Provider store={store}>
      <Switch>
          <Route path="/" render={() => <Redirect to="/todomvc" />} exact />
          <Route path="/todomvc" component={Todomvc} />
          <Route path="/home" component={Home} />
          <Route path="/page" component={Page} />
      </Switch>
    </Provider>
  </Router>,
  document.getElementById('root')

我的想法是能有一个函数,当页面第一次进入路由的时候也能触发这个函数。

commented

提的位置不对