AlthoughThe support for TypeScript is not very complete, but from the upcoming version 3.0 this year, the repository on GitHubvue-next Look, providing better official support for TS should also be an important feature. So, before we welcome 3.0, let’s take a look at the current version of the two to eat~
Create a project
- Although there are various related Starters on GitHub, use
Vue CLI
It should be a relatively good way to use itvue create
When creating a new project,preset
chooseManually select features
Options, addTypeScript
- If you want to fully use the class features provided in ES6 in vue applications, then
class-style component syntax
Select Y (this article mainly introduces the situation of selecting Y) - for
Babel
In other words, generally speaking,linter / formatter
The specific choices can be determined according to the project requirements, and there will be no explanation here
Enter the project
After creation is completed, take a look, can be found
vue-class-component
andvue-property-decorator
And other ts-related modules have been added, where:vue-class-component
This allows you to create components using class-style syntax, such as the following code:
<template> <div> <button @click="decrement">-</button> {{ count }} <button @click="increment">+</button> </div> </template> <script lang="ts"> import Vue from 'vue' import Component from 'vue-class-component' // Define the component in class-style @Component export default class Counter extends Vue { // Class properties will be component data count = 0 // Methods will be component methods increment() { ++ } decrement() { -- } } </script>
andvue-property-component
It depends entirely on the former and provides@Component
Several other decorators, such as@Prop
import { Vue, Component, Prop } from 'vue-property-decorator' @Component export default class YourComponent extends Vue { @Prop(Number) readonly propA: number | undefined @Prop({ default: 'default value' }) readonly propB!: string @Prop([String, Boolean]) readonly propC: string | boolean | undefined }
Let’s take another simple example of combining the two:
<template> <div class="hello"> <h1>{{ msg }}</h1> <h1>{{ fullName }}</h1> <button @click="reverseStr()">Reverse</button> </div> </template> <script lang="ts"> import { Component, Prop, Vue, Watch } from 'vue-property-decorator'; @Component export default class HelloWorld extends Vue { @Prop() private msg!: string; firstName = "rapt"; lastName = "azure"; mounted() { ('mounted'); } // Computed property get fullName(): string { return + ; } // Method reverseStr() { = ('').reverse().join(''); = ('').reverse().join(''); } } </script>
- At this time, your vue project is already fully-typed, and of course there will be better automatic completion and error prompts.
- To better determine the type, for example
interfaces
Such folders make full use of ts' interfaces and classes to make the project have better organizational structure, readability and maintenance.
Another option
In fact, of course, you can not use the class style. In this way, it will be more similar to the familiar vue, and of course it also fully supports the types.
Here is a simple example~<template>
<div class="hello"> <h1>{{ msg }}</h1> <h1>{{ test }}</h1> </div> </template> <script lang="ts"> import Vue from 'vue'; export default ({ name: 'HelloWorld', props: { msg: String, }, data() { return { test: "Hello from TS" as string } }, methods: { pressMe(): string { return + 'br' } } }); </script>
Other words
- This article just briefly discusses the possibility of using TypeScript in it, and more related content isOfficial DocumentationYou can find it in, or you can go to the Vue area of Github and TS area to visit~
- The emergence of TypeScript has brought new vitality to the JavaScript ecosystem. Whether it is the three front-end frameworks Vue, React, Angular, or Node-based back-end frameworks such as Nest and Express, they are actively embracing TS. I hope the entire ecosystem will develop better and better in the future~
Summarize
This is the end of this article about using TypeScript. For more related content on using TypeScript, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!