2016-09-08 6 views
0

Ich habe eine hybride reagieren/angularjs App. Wenn ich zu Routen route, die in Reagieren nicht vorhanden sind, muss ich eine vollständige Seite neu laden, damit der Server die eckigen Seiten zurückgeben kann. Ich sehe den Fehler geworfen Warning: [react-router] Location '/foo-bar' did not match any routes. Wie kann ich das abfangen und eine vollständige Seite neu laden, wenn es passiert?Catch "Warnung: [react-router] Ort '/ foo-bar' hat keine Routen" und eine volle Seite neu laden, wenn es geworfen wird

Antwort

1

es heraus ...

const reloadPage = (nextState, replace, callback) => { 
    callback("Route not found"); 
    window.location.reload(); 
}; 

const routes = (
    <Route path="/" component={Chrome}> 
    <IndexRoute component={Home}/> 
    <Route path="catagory" component={CategoryPage}/> 
    <Route path="*" onEnter={reloadPage}/> 
    </Route> 
); 

try { 
    ReactDOM.render((
     <Router history={browserHistory}>{routes}</Router> 
    ), 
    rootEl 
); 
} catch (err) { 
    if (err !== "Route not found") { 
    throw err; 
    } 
} 

Für react-router v3 diese

// From the react-router docs: "If `callback` is listed as a 3rd argument, 
// this hook will run asynchronously, and the transition will block 
// until `callback` is called." 
const reloadPage = (nextState, replace, callback) => { 
    window.location.reload(); 
}; 

const routes = (
    <Route path="/" component={Chrome}> 
    <IndexRoute component={Home}/> 
    <Route path="catagory" component={CategoryPage}/> 
    <Route path="*" onEnter={reloadPage}/> 
    </Route> 
); 

ReactDOM.render((
    <Router history={browserHistory}>{routes}</Router> 
), 
    rootEl 
); 
wird
Verwandte Themen