這些都是非常基本的問題,但universal-router
的文檔沒有解釋這一點,它給出的例子在我的React項目中似乎不起作用。在哪裏把通用路由器放入React以及如何定義鏈接?
我有這個在我index.js
:
import Router from 'universal-router';
import { HomeComponent, HelpComponent, AboutComponent } from './home.jsx';
const routes = [
{ path: '/', action:() => <HomeComponent /> },
{ path: '/home', action:() => <HomeComponent /> },
{ path: '/about', action:() => <AboutComponent /> },
{ path: '/help', action:() => <HelpComponent /> },
{ path: '*', action:() => <h1>Nope.</h1> }
];
const router = new Router(routes);
router.resolve({ path: '/' }).then(component => {
ReactDOM.render(component, document.getElementById('router-outlet'));
});
router.resolve({ path: '/help' }).then(component => {
ReactDOM.render(component, document.getElementById('router-outlet'));
});
router.resolve({ path: '/about' }).then(component => {
ReactDOM.render(component, document.getElementById('router-outlet'));
});
這在我App
組件,一個很簡單的菜單:
export class App extends React.Component {
render() {
return(
<div>
<a href="/">Root</a> |
<a href="/home">Home</a> |
<a href="/help">Help</a> |
<a href="/about">About</a> |
<a href="/uijsklfdsse">404</a>
</div>
);
}
}
正如你看到的,這是非常簡單的,我只是想來計算如何讓這個工作。它幾乎正是寫入here,但它不起作用。出於某種原因,它總是被調用的AboutComponent
,並且當我點擊一個鏈接時,瀏覽器似乎刷新了整個頁面,因此它不像SPA。我有一種感覺,我把路線定義放在錯誤的地方,但我應該把它放在哪裏?這是我應該如何定義鏈接?
我使用的是universal-router
,因爲React-router
在安裝時表示它已被棄用並由此替換。讓我知道這是不是一個好主意。
我做到了。無論實際路徑如何,解決方案總是會引發火災。我實際上放棄了整個事情,現在就使用不同的路由器。我浪費了一整天的時間。 –