-
Notifications
You must be signed in to change notification settings - Fork 17
Docs Reshuffle & Mini Brain Dump #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
39c0be5
Moved class components to a dedicated page
chiubaca d31fa1f
Moved over section re: props and class components over from readme.md
chiubaca 7963ad9
-Added link to class components md
chiubaca 3908106
Expanded class components props example
chiubaca ce4175b
Included alternative example to annoate props with anon function
chiubaca 6166ede
Typing ref's examples
chiubaca 81e087e
Added video by Modus Create, Inc to resources
chiubaca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Class Components | ||
|
||
## Overview | ||
|
||
[Vue Class Components](https://class-component.vuejs.org/) offers an alternative class-style syntax for Vue components which integrates well with TypeScript. | ||
|
||
To have consistent support for decorators in your Vue components, it's also recommended to install [vue-property-decorator](https://github.com/kaorun343/vue-property-decorator). | ||
|
||
|
||
To get started with both libraries in your existing Vue project, run: | ||
``` | ||
npm install vue-class-component vue-property-decorator | ||
``` | ||
|
||
You only need to import `vue-property-decorator` into your `.vue` file as it extends `vue-class-component`. | ||
|
||
You can now write TS in your components like this: | ||
|
||
```vue | ||
<template> | ||
<div> | ||
{{ count }} | ||
<button v-on:click="increment">+</button> | ||
<button v-on:click="decrement">-</button> | ||
{{ computedValue }} | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Vue, Component } from "vue-property-decorator"; | ||
|
||
@Component | ||
export default class Hello extends Vue { | ||
|
||
count: number = 0 | ||
vue: string = "vue" | ||
ts: string = "ts" | ||
|
||
// Lifecycle methods can be accessed like this | ||
mounted() { | ||
console.log('component mounted') | ||
} | ||
|
||
// Method are component methods | ||
increment(): void { | ||
this.count++ | ||
} | ||
|
||
decrement(): void { | ||
this.count-- | ||
} | ||
|
||
// Computed values are getters | ||
get computedValue(): string { | ||
return `${vue} and ${ts} rocks!` | ||
} | ||
} | ||
</script> | ||
``` | ||
See the [full guide for Vue Class Components](https://class-component.vuejs.org/guide/class-component.html#data). | ||
|
||
> _Class components should not confused with the now abandoned [Class API proposal](https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121)._ | ||
|
||
## Props | ||
You can use the `Prop` decorator to annoate your prop types like so: | ||
|
||
```ts | ||
<script lang="ts"> | ||
import { Vue, Component, Prop } from "vue-property-decorator"; | ||
|
||
interface PersonInfo { | ||
firstName: string, | ||
surname: string, | ||
age: number | ||
} | ||
|
||
@Component | ||
export default class InfoCard extends Vue { | ||
@Prop() readonly info!: PersonInfo; | ||
@Prop({ default: false }) readonly admin?: boolean; | ||
} | ||
</script> | ||
``` | ||
Is equivalent to: | ||
|
||
```ts | ||
import Vue from "vue-property-decorator"; | ||
import Vue, { PropType } from 'vue' | ||
|
||
interface PersonInfo { | ||
firstName: string, | ||
surname: string, | ||
age: number | ||
} | ||
export default { | ||
props: { | ||
info: { | ||
type: Object as PropType<PersonInfo>, | ||
required: true | ||
}, | ||
admin: { | ||
type: Boolean, | ||
default: false | ||
} | ||
}, | ||
} | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.