fix: improve type safety and error handling in arkts-development
- Replace Record<string, Object> with specific interface types for router params - Add error handling catch block to list-page-template.ets - Add clarification note for router.back() with params - Improve type safety following ArkTS best practices
This commit is contained in:
@@ -96,7 +96,11 @@ router.replaceUrl({ url: 'pages/New' });
|
|||||||
router.back();
|
router.back();
|
||||||
|
|
||||||
// Get params
|
// Get params
|
||||||
const params = router.getParams() as Record<string, Object>;
|
interface RouteParams {
|
||||||
|
id: number;
|
||||||
|
title?: string;
|
||||||
|
}
|
||||||
|
const params = router.getParams() as RouteParams;
|
||||||
```
|
```
|
||||||
|
|
||||||
## Network Request
|
## Network Request
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ struct {{PageName}} {
|
|||||||
{ id: '1', title: 'Item 1', description: 'Description 1' },
|
{ id: '1', title: 'Item 1', description: 'Description 1' },
|
||||||
{ id: '2', title: 'Item 2', description: 'Description 2' },
|
{ id: '2', title: 'Item 2', description: 'Description 2' },
|
||||||
];
|
];
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load items:', error);
|
||||||
|
// TODO: Show error message to user
|
||||||
} finally {
|
} finally {
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,17 +70,24 @@ router.back({
|
|||||||
url: 'pages/HomePage',
|
url: 'pages/HomePage',
|
||||||
params: { result: 'success' }
|
params: { result: 'success' }
|
||||||
});
|
});
|
||||||
|
// Note: Previous page receives params via router.getParams()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Get Parameters
|
### Get Parameters
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
// Define expected params interface
|
||||||
|
interface PageParams {
|
||||||
|
id: number;
|
||||||
|
title?: string;
|
||||||
|
}
|
||||||
|
|
||||||
// In target page
|
// In target page
|
||||||
aboutToAppear(): void {
|
aboutToAppear(): void {
|
||||||
const params = router.getParams() as Record<string, Object>;
|
const params = router.getParams() as PageParams;
|
||||||
if (params) {
|
if (params) {
|
||||||
const id = params['id'] as number;
|
const id = params.id;
|
||||||
const title = params['title'] as string;
|
const title = params.title;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user