2017-10-10 76 views
0

有以下「應用程序」,並且window.location不可配置。理想情況下,我希望點擊提交按鈕並轉到備用域。這實際上是一個較大應用程序的一部分,我在其中使用外部身份提供程序進行登錄。以下摘錄是一個完全工作的樣本來說明這個問題:window.location不能與反應應用程序一起使用反應路由器4

import React from 'react'; 
import { render } from 'react-dom'; 
import { Provider } from 'react-redux'; 
import { createStore } from 'redux'; 
import Router from './routes'; 
import reducers from './reducers'; 
import { BrowserRouter, Route, Switch } from 'react-router-dom'; 

const store = createStore(reducers); 

class Signin extends React.Component { 
    onSubmit() { 
    var result = Object.getOwnPropertyDescriptor(window, 'location'); 
    window.location = 'http://www.bbc.co.uk'; 
    } 

    render() { 
    return (
     <div> 
     <form 
      className="form-horizontal col-xs-10 col-xs-offset-1" 
      onSubmit={this.onSubmit.bind(this)} 
     > 
      <div className="form-group"> 
      <span className="col-xs-3" /> 
      <div className="col-sm-6"> 
       <button type="submit" className="btn btn-success btn-sm"> 
       Submit 
       </button> 
      </div> 
      </div> 
     </form> 
     <script /> 
     </div> 
    ); 
    } 
} 

class App extends React.Component { 
    render() { 
    return (
     <div> 
     <div className="container">{this.props.children}</div> 
     </div> 
    ); 
    } 
} 

render(
    <Provider store={store}> 
    <BrowserRouter> 
     <App> 
     <Switch> 
      <Route exact path="/" component={Signin} /> 
     </Switch> 
     </App> 
    </BrowserRouter> 
    </Provider>, 
    document.getElementById('root') 
); 

你可以看到一個working sample here

在我的真正的「應用程序」,使用下面的依賴關係和版本是:

"prop-types": "^15.6.0" 
    "react": "^15.6.1" 
    "react-dom": "^15.6.1" 
    "react-redux": "4.3.0" 
    "react-router-dom": "^4.0.0" 
    "react-scripts": "1.0.13" 

你會注意到我寫了一行:

var result = Object.getOwnPropertyDescriptor(window, 'location'); 

這將返回信息關於location對象,並指示configurable=false。目前的網址與最後標記爲/?的網址保持一致。

任何人都可以提供任何見解嗎?也試過window.location.href。似乎可能反應/反應路由器正在重新配置window.location是不可配置的 - 是否有任何理由或任何人可以提供的解決方法。

回答

1

好的,我沒有壓制表單提交。

onSubmit(e) { 
    e.preventDefault(); 
    window.location = "https://www.bbc.co.uk"; 
    } 

是的,這一個又......