Skip to content

Commit 6eaa62c

Browse files
committed
Part 4 - Allow vue components to be written in Typescript
To have vue components working with Typescript, we just need to use the vue-property-decorator library. In this commit I have also added a few features to demonstrate how to properly use Typescript with vue. At this point, we have the entire front-end written in Typescript. In the next commit, we'll set up jest to test our Vue components.
1 parent c47213d commit 6eaa62c

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"ts-loader": "^5.4.3",
2424
"typescript": "^3.4.5",
2525
"vue": "^2.5.17",
26+
"vue-property-decorator": "^8.1.0",
2627
"vue-template-compiler": "^2.6.10"
2728
}
2829
}

resources/js/components/ExampleComponent.vue

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@
66
<div class="card-header">Example Component</div>
77

88
<div class="card-body">
9-
I'm an example component.
9+
Hello {{userName}}, your name backwards is "{{backwardsUserName}}".
1010
</div>
1111
</div>
1212
</div>
1313
</div>
1414
</div>
1515
</template>
1616

17-
<script>
18-
export default {
19-
mounted() {
20-
console.log('Component mounted.')
17+
<script lang="ts">
18+
import {Component, Prop, Vue, Watch} from 'vue-property-decorator';
19+
20+
@Component
21+
export default class ExampleComponent extends Vue {
22+
// Props are defined using the Prop decorator
23+
@Prop({default: 'Unknown'}) protected userName!: string;
24+
25+
// Life cycle methods are declared just like regular methods
26+
private mounted(): void {
27+
console.log('Component mounted.');
28+
}
29+
30+
// Computed properties are written as getters
31+
protected get backwardsUserName(): string {
32+
return this.userName.split('').reverse().join('');
33+
}
34+
35+
// Watchers become much cleaner with Typescript decorators, just place the decorator above
36+
// the function that will run.
37+
@Watch('userName')
38+
private onNameChanged(newName: string): void {
39+
alert(`I see that you changed your name! ${newName} it is then.`);
2140
}
2241
}
2342
</script>

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,6 +6265,11 @@ [email protected]:
62656265
dependencies:
62666266
indexof "0.0.1"
62676267

6268+
vue-class-component@^7.0.1:
6269+
version "7.0.2"
6270+
resolved "https://registry.yarnpkg.com/vue-class-component/-/vue-class-component-7.0.2.tgz#c5f35a91c0e9341532392b84d606a84911fb13bc"
6271+
integrity sha512-8xw/wkZI2tgHcwvkSRC1ax7GeP1CG27wKhedvOAdjdASm05VU4RijGsCYti6s6CzBioBL5BQUmntQQTCsp1wnQ==
6272+
62686273
vue-hot-reload-api@^2.3.0:
62696274
version "2.3.3"
62706275
resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.3.tgz#2756f46cb3258054c5f4723de8ae7e87302a1ccf"
@@ -6281,6 +6286,13 @@ vue-loader@^15.4.2:
62816286
vue-hot-reload-api "^2.3.0"
62826287
vue-style-loader "^4.1.0"
62836288

6289+
vue-property-decorator@^8.1.0:
6290+
version "8.1.0"
6291+
resolved "https://registry.yarnpkg.com/vue-property-decorator/-/vue-property-decorator-8.1.0.tgz#66493a5350e7f643e852e7698ec2c883554daa79"
6292+
integrity sha512-TUWpbadApSW/sx9hlbrUq092ULm4E3RkL5X4fFhkiJ88/Y99lVubjh3bd3VbFQ8JRlKaTeqMOKaFHQRzWBCFPg==
6293+
dependencies:
6294+
vue-class-component "^7.0.1"
6295+
62846296
vue-style-loader@^4.1.0:
62856297
version "4.1.2"
62866298
resolved "https://registry.yarnpkg.com/vue-style-loader/-/vue-style-loader-4.1.2.tgz#dedf349806f25ceb4e64f3ad7c0a44fba735fcf8"

0 commit comments

Comments
 (0)