面试题答案
一键面试文件结构
在Qwik路由系统中,routes
目录起着关键作用。所有与路由相关的文件都放在此目录下。例如,一个典型的项目结构可能像这样:
my - qwik - project
├── src
│ ├── routes
│ │ ├── index.tsx
│ │ ├── about.tsx
│ │ └── contact.tsx
│ └── ...
└── ...
这里 index.tsx
通常对应网站的根路由,about.tsx
对应 /about
路由,contact.tsx
对应 /contact
路由。
配置语法
Qwik 路由主要通过文件系统约定来实现路由配置,而不是传统的配置文件。每个在 routes
目录下的 .tsx
文件对应一个路由。
简单静态路由配置示例
假设我们要配置一个简单的静态路由 /products
:
- 在
src/routes
目录下创建一个名为products.tsx
的文件。 - 在
products.tsx
文件中编写如下代码:
import { component$ } from '@builder.io/qwik';
export default component$(() => {
return (
<div>
<h1>Products Page</h1>
<p>This is the products page content.</p>
</div>
);
});
这样就配置好了一个 /products
的静态路由,当用户访问 /products
时,就会看到 products.tsx
文件渲染的内容。