@vureact/compiler-core 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ # 路由适配指南
2
+
3
+ ## 概述
4
+
5
+ VuReact 对 Vue Router 提供了完整的转换支持,但由于路由是工程级上下文,编译后仍需要一些人工调整。
6
+
7
+ ### 自动转换的部分
8
+
9
+ - `<router-link>` → `<RouterLink>`
10
+ - `<router-view>` → `<RouterView>`
11
+ - 路由API调用:`useRouter()`, `useRoute()`等
12
+ - 自动注入`@vureact/router`依赖
13
+
14
+ ### 需要人工调整的部分
15
+
16
+ - 路由配置文件格式转换
17
+ - 入口文件渲染方式
18
+ - 排除配置设置
19
+
20
+ ## 配置步骤
21
+
22
+ ### 步骤1:编译前的准备
23
+
24
+ 确保你的Vue项目使用标准Vue Router配置。
25
+
26
+ ### 步骤2:执行编译
27
+
28
+ ```bash
29
+ npx vureact build
30
+
31
+ # 或手动配置 npm 命令
32
+ npm run vr:build
33
+ ```
34
+
35
+ ### 步骤3:调整输出的 React 项目(关键步骤)
36
+
37
+ #### 3.1 更新入口文件(src/main.tsx)
38
+
39
+ 示例:
40
+
41
+ ```tsx
42
+ import { StrictMode } from 'react';
43
+ import { createRoot } from 'react-dom/client';
44
+ import './index.css';
45
+ import router from './router';
46
+
47
+ createRoot(document.getElementById('root')!).render(
48
+ <StrictMode>
49
+ <router.RouterProvider />
50
+ </StrictMode>,
51
+ );
52
+ ```
53
+
54
+ **重要变化**:
55
+
56
+ - 导入路由提供器,不需要渲染 `<App />`,因为 `App` 应该在路由配置中挂载。
57
+
58
+ #### 3.2 转换路由配置文件
59
+
60
+ 实际以你项目输出的路由配置结构为准。
61
+
62
+ 将 `src/router/index.ts` 改为 `src/router/index.tsx`。
63
+
64
+ 示例:
65
+
66
+ ```tsx
67
+ import { createRouter, createWebHashHistory } from '@vureact/router';
68
+ import App from '../App';
69
+ import Dashboard from '../pages/Dashboard';
70
+ import Customers from '../pages/Customers';
71
+
72
+ const router = createRouter({
73
+ history: createWebHashHistory(),
74
+ routes: [
75
+ {
76
+ path: '/',
77
+ component: <App />,
78
+ children: [
79
+ { path: 'dashboard', component: <Dashboard /> },
80
+ { path: 'customers', component: <Customers /> },
81
+ ],
82
+ },
83
+ ],
84
+ });
85
+
86
+ export default router;
87
+ ```
88
+
89
+ **重要变化**:
90
+
91
+ - 文件扩展名:`.ts` → `.tsx`
92
+ - 导出 `createRouter` 路由器实例
93
+ - 组件语法:`component: Dashboard` → `component: <Dashboard />`
94
+ - 导入方式:保持React组件导入
95
+
96
+ #### 3.3 配置排除项
97
+
98
+ 在 `vureact.config.js` 中添加排除配置,防止重新编译时覆盖手动调整的文件。
99
+
100
+ 若后续需要修改这些文件,需**手动同步**变更。
101
+
102
+ 示例:
103
+
104
+ ```js
105
+ export default defineConfig({
106
+ exclude: [
107
+ 'src/main.ts',
108
+ 'src/router/**', // 排除整个路由目录
109
+ ],
110
+ });
111
+ ```
112
+
113
+ ### 步骤4:验证与测试
114
+
115
+ 1. 安装依赖:`npm install`
116
+ 2. 启动项目:`npm run dev`
117
+ 3. 测试路由跳转
118
+ 4. 验证嵌套路由
119
+
120
+ ## 常见问题
121
+
122
+ ### Q1: 页面空白,控制台报错
123
+
124
+ **可能原因**:路由配置文件未转换为JSX语法
125
+ **解决方案**:
126
+
127
+ 1. 确认文件扩展名为`.tsx`或`.jsx`
128
+ 2. 检查组件是否使用`<Component />`语法
129
+ 3. 确保正确导入React组件
130
+
131
+ ### Q2: 路由跳转404
132
+
133
+ **可能原因**:历史模式配置错误
134
+ **解决方案**:
135
+
136
+ - 检查`createWebHashHistory` vs `createWebHistory`
137
+ - 确认base路径配置
138
+
139
+ ### Q3: 嵌套路由不显示内容
140
+
141
+ **可能原因**:父组件缺少`<RouterView />`
142
+ **解决方案**:
143
+ 在布局组件中添加路由出口:
144
+
145
+ ```vue
146
+ <!-- Vue原版 -->
147
+ <template>
148
+ <div>
149
+ <header>...</header>
150
+ <router-view />
151
+ </div>
152
+ </template>
153
+ ```
154
+
155
+ ### Q4: 编译后手动调整被覆盖
156
+
157
+ **可能原因**:未将文件加入`exclude`列表
158
+ **解决方案**:
159
+ 更新`vureact.config.js`,将调整过的文件加入排除列表。
160
+
161
+ ## 最佳实践
162
+
163
+ ### 1. 路由配置规范
164
+
165
+ - 导出路由器实例:`export default createRouter({})`
166
+ - 使用命名路由:`{ name: 'dashboard', path: '/dashboard', ... }`
167
+ - 配置路由懒加载:
168
+
169
+ ```tsx
170
+ const Dashboard = lazy(() => import('../pages/Dashboard'));
171
+ ```
172
+
173
+ - 统一管理路由meta字段
174
+
175
+ ### 2. 文件管理策略
176
+
177
+ - 路由配置文件单独目录管理
178
+ - 类型定义分离到`src/router/types.ts`
179
+ - 路由守卫统一在`src/router/guards.ts`
180
+
181
+ ### 3. 测试策略
182
+
183
+ - 单元测试路由配置
184
+ - E2E测试路由跳转流程
185
+ - 编译前后功能对比测试
186
+
187
+ ## 相关资源
188
+
189
+ - [VuReact 官方文档](https://vureact.top)
190
+ - [VuReact Router 官方文档](https://router.vureact.top)
191
+ - [编译问题反馈与支持](https://github.com/vureact-js/core/issues)
192
+ - [路由问题反馈与支持](https://github.com/vureact-js/vureact-router/issues)