Skip to content

Commit

Permalink
feat: add best practice code for data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
kkukelka committed Apr 1, 2022
1 parent 2b94454 commit 209a06c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions STYLE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ Use shorthands for vue attributes
- `@click` instead of `v-on:click`
- ...


### Fetching Data
Though we haven't yet transitioned most of our data fetching logic to Nuxt lifecycles, the following syntax should be considered best practice:
```typescript
// pages
import { Component } from 'nuxt-property-decorator'

@Component({
async asyncData({ app, $config, params }) {
const res = await app?.apolloProvider?.clients[$config.prefix].query({
query: queryGql,
variables: {
id: params.id
},
})

return {
data: res.data,
total: res.total,
}
}
})
export default class ClassName extends Vue {
data?: Type
total?: Type

[...]
}
```


### Reusability Through Abstraction
If your component will be used on several occasions in many different contexts, you should think about the way you pass data to your components and the way events are handled.
In regards to event handling, you should always aim to emit events happening in your component, while the handling should be done in the parent or page itself. Thereby, the click of a button can trigger all kinds of functionality, without having to be aware of its context.
Expand Down

0 comments on commit 209a06c

Please sign in to comment.