Skip to content

Commit 7963ad9

Browse files
committed
-Added link to class components md
- Included section on Computed Properties
1 parent d31fa1f commit 7963ad9

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

README.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Cheatsheets for experienced Vue developers getting started with TypeScript.
44

55
- [Vue 3 specifics](vue-3.md)
6+
- [Class Components & Decorators](class-components.md)
67

78
# Section 1: Setup
89

@@ -116,26 +117,7 @@ export default Vue.extend({
116117
</script>
117118
```
118119

119-
With vue-class-components and vue-property-decorator, you can use the `Prop` decorator:
120-
121-
```vue
122-
<script lang="ts">
123-
import { Vue, Component, Prop } from "vue-property-decorator";
124-
125-
interface PersonInfo {
126-
firstName: string,
127-
surname: string,
128-
age: number
129-
}
130-
131-
@Component
132-
export default class InfoCard extends Vue {
133-
@Prop({ required: true }) readonly info: PersonInfo;
134-
}
135-
</script>
136-
```
137-
138-
## Data Properties
120+
## Data Properties (Options API)
139121

140122
You can enforce types on Vue data properties by annotating the return data object:
141123

@@ -182,6 +164,30 @@ export default Vue.extend({
182164
```
183165
Note that [type assertion](https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions) like this does not provide any type safety. If for example, the `contents` property was missing in `newPost`, TypeScript would not catch this error.
184166

167+
## Computed Properties (Options API)
168+
169+
Typing the return type for your computed properties is important especially when `this` is involved as TypeScript sometimes has trouble infering the type.
170+
171+
```ts
172+
173+
export default Vue.extend({
174+
data() {
175+
return {
176+
name: 'World',
177+
}
178+
},
179+
computed: {
180+
greet(): string { //👈 Remember to annotate your computed properties like so.
181+
return 'Hello ' + this.name
182+
},
183+
}
184+
})
185+
186+
```
187+
188+
>
189+
190+
185191
# Other Vue + TypeScript resources
186192
- Views on Vue podcast - https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/
187193
- Focuses a lot on class components and vue-property-decorator - https://blog.logrocket.com/how-to-write-a-vue-js-app-completely-in-typescript/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy