- 实现步骤:
- 在Qwik项目中,首先确保安装了必要的路由相关依赖(通常Qwik自带一些路由功能)。
- 配置路由规则,告诉应用程序在不同路径下应渲染哪个组件。
- 在页面中创建链接,使用路由功能来实现页面间导航。
- 关键代码示例:
import { component$, qwikCity } from '@builder.io/qwik-city';
import { HomePage } from './HomePage';
import { AboutPage } from './AboutPage';
export const routes = [
{ path: '/', component: component$(HomePage) },
{ path: '/about', component: component$(AboutPage) }
];
export default qwikCity({ routes });
- 在页面中创建导航链接(例如在
HomePage
组件中添加到AboutPage
的链接):
import { component$, Link } from '@builder.io/qwik';
export default component$(() => {
return (
<div>
<h1>Home Page</h1>
<Link href="/about">Go to About Page</Link>
</div>
);
});
- 在
AboutPage
组件中添加返回HomePage
的链接:
import { component$, Link } from '@builder.io/qwik';
export default component$(() => {
return (
<div>
<h1>About Page</h1>
<Link href="/">Go to Home Page</Link>
</div>
);
});