You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/composition-api.md
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -779,6 +779,21 @@ export default new Router({
779
779
780
780
Now, the `Post.vue` file only requires to have a `prop` named `id`. Vue Router will pass the params from the route as props to the component. See the [first useGet example](#useget) for a component that would work with the above route. The vue-router documentation has more information about [Passing Props to Route Components](https://router.vuejs.org/guide/essentials/passing-props.html#passing-props-to-route-components)
781
781
782
+
### Composing with Model types
783
+
784
+
Both `useGet` and `useFind` have an optional type parameter for the Model type which is used as the type for the returned item(s).
785
+
786
+
```ts
787
+
// Destructure Model class from global models object
// Just like before, we define our class as an extension of BaseModel
64
+
exportclassUserextendsBaseModel { /* ... */ }
65
+
66
+
// Augment the User Model interface
67
+
exportinterfaceUser {
68
+
email:string
69
+
password:string
70
+
}
69
71
```
70
72
71
-
The proper way to edit an existing `User` instance is to clone it, edit the *clone's* props, and then commit the changes.
72
-
This ensures changes don't propagate to the rest of the app until ready.
73
+
Now, whenever we access a `User` model, all fields defined in the interface will be available in IDE auto-complete/intellisense along with the model methods/props.
74
+
75
+
If you already have a User interface defined under a different name, just define a new interface with the same name as your Model class like so
// if our User interface already exists as UserRecord
79
+
exportinterfaceUserextendsUserRecord {}
79
80
```
80
81
81
-
You can disable this `readonly` behavior if desired. In `feathers-client.ts`, augment FeathersVuex's typing
82
+
To further enhance typing, you can augment FeathersVuex types to aid development in other parts of your app. It's important to note the differences in the following example if you do or do not setup a `serverAlias`.
82
83
83
84
```ts
84
-
declaremodule'feathers-vuex' {
85
-
interfaceFeathersVuexTypeOptions {
86
-
'model-readonly':false
87
-
}
88
-
}
89
-
```
85
+
// src/store/user.store.ts
86
+
import { ServiceState } from'feathers-vuex'
90
87
91
-
### Casting the BaseModel <Badgetext="3.11.0+" />
92
-
93
-
Typescript users can further enhance typing on Model classes and instances by passing their data's underlying structure as an interface
94
-
to `castBaseModel<T>()`. This gives helpful type hints and autocomplete from your IDE when interacting with your underlying Model data.
88
+
exportclassUserextendsBaseModel { /* ... */ }
89
+
exportinterfaceUser { /* ... */ }
90
+
const servicePath ='users'
95
91
96
-
To take advantage of this, first we need to update `feathers-client.ts` to export the new function
92
+
declaremodule"feathers-vuex" {
93
+
interfaceFeathersVuexStoreState {
94
+
[servicePath]:ServiceState<User>
95
+
}
97
96
98
-
```ts
99
-
// feathers-client.ts
100
-
const {/* other props, */ castBaseModel } =feathersVuex(/* ... */)
97
+
// Only if you setup FeathersVuex without a serverAlias!!
98
+
interfaceFeathersVuexGlobalModels {
99
+
User:typeofUser
100
+
}
101
+
}
101
102
102
-
// Export `castBaseModel` too
103
-
export { /* other props */castBaseModel }
103
+
// Only if you setup FeathersVuex with a serverAlias!!
104
+
declaremodule"src/store" {
105
+
interfaceMyApiModels {
106
+
User:typeofUser
107
+
}
108
+
}
104
109
```
105
110
106
-
Now we can use `castBaseModel()` when defining our Model class
111
+
If you have setup a `serverAlias`, you need to add the following to `src/store/index.ts`.
0 commit comments