从网址中删除查询参数

时间:2019-05-27 11:19:29

标签: javascript reactjs react-router query-parameters

我有以下网址:

http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860

我想重定向到:

http://my.site#/homepage

我这样做是:

import { push } from 'react-router-redux'

...

dispatch(push('/homepage'))

但是反应带我去:

http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860#/homepage

我如何告诉React将查询参数放在浏览器的地址栏中而无需重新加载应用程序

4 个答案:

答案 0 :(得分:0)

尝试这种方式(硬编码方式)

import { push } from 'react-router-redux'

...
const url = `${window.location.origin}/homepage`;
dispatch(push(url));

或者这个

history.location.pathname =`${window.location.origin}/homepage`;

这些方法根本不是一个好习惯,但是可以起作用(肯定是第二种方法)

Read more

答案 1 :(得分:0)

在以下链接中,我git克隆了该文件,并在主目录和示例基本目录中运行了npm install,然后运行npm开始获得了有效的示例。

https://github.com/reactjs/react-router-redux

https://github.com/reactjs/react-router-redux/tree/master/examples/basic

https://github.com/reactjs/react-router-redux#pushlocation-replacelocation-gonumber-goback-goforward

删除查询参数(靠近底部)并验证是否已删除的代码:

import { createStore, combineReducers, applyMiddleware } from 'redux'
import { browserHistory } from 'react-router'
import { syncHistoryWithStore, routerReducer, routerMiddleware, push } from 'react-router-redux'
import * as reducers from './reducers'

const reducer = combineReducers({
  ...reducers,
  routing: routerReducer
})

const middleware = routerMiddleware(browserHistory)

const store = createStore(
  reducer,
  applyMiddleware(middleware)
)
const history = syncHistoryWithStore(browserHistory, store)

// Dispatch from anywhere like normal.
store.dispatch(push("/?test=ok"));
// store2.dispatch(push(history.createHref("/")));
var count = 0;

history.listen(location => {
    console.log(location);
    if (count > 1) { return; }
    count++;
    store.dispatch(push({ pathname:'/', search: '' })); // Drops the params
});

检查控制台,您会看到查询参数(搜索)字段为空

答案 2 :(得分:0)

请参阅我的路由器部分  

 <div>
                <MainHeader />
                <Switch>
                    <Route exact path='/:pickupLocationName/:pickupDate/:pickupTime/:returnDate/:returnTime' render={(props) => <Quote  {...props} />} />
                    <Route path='/BookerInitial/:user/:id' component={BookerInitial} />
                    <Route path='/VehicleList' component={VehicleList} />
                    <Route path='/AdditionalCoverages' component={AdditionalCoverages} />
                    <Route path='/BookingDetails' component={BookingDetails} />
                    <Route path='/RenterInfo' component={RenterInfo} />
                </Switch>
                <Footer/>
            </div>

BookerInitial页面中有下一个按钮

<button className="submit btn-skew-r btn-effect" id="nextBtn" style={{ backgroundColor: "#da1c36" }} onClick={this.handleSubmit}  >Next</button>

提交按钮方法

 handleSubmit = () => {
      this.props.history.push('./VehicleList');

    }

我也遇到了这个问题。终于我找到了解决方案 我更改为this.props.history.push('/ VehicleList'); 从this.props.history.push('./ VehicleList'); 上面您可以看到dot(。)是我的代码存在的问题。因此无需通过代码删除网址中的参数。 谢谢

答案 3 :(得分:0)

使用正则表达式,此解决方案可能会让您更轻松:

  var oldURL = 'http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860'
  var newURL = /(http(s?).+)(\?.+)/.exec(oldDomain)[1] + 'homepage'

您可以在项目中使用newURL。