官术网_书友最值得收藏!

  • Vue.js 3 Cookbook
  • Heitor Ramon Ribeiro
  • 448字
  • 2021-06-11 18:11:56

Creating your first TypeScript Vue component with vue-class-component

As Vue components are object-based and have a strong relationship with the this keyword of the JavaScript object, it gets a little bit confusing to develop a TypeScript component.

The vue-class-component plugin uses the ECMAScript decorators proposal to pass the statically typed values directly to the Vue component and makes the process of the compiler understand what is happening more easily.

Getting ready

The pre-requisite for this recipe is as follows:

  • Node.js 12+

The Node.js global objects that are required are as follows:

  • @vue/cli
  • @vue/cli-service-global

How to do it...

First, we need to create our Vue CLI project. We can use the one we created in the last recipe or start a new one. To find how to create a Vue CLI project with TypeScript, please check the 'Adding TypeScript to a Vue CLI project' recipe.

Follow the instructions to create your first Vue component with Typescript and vue-class-component:

  1. Create a new file inside the src/components folder, called Counter.vue.
  2. Now, let's start making the script part of the Vue component. We will make a class that will have data with a number, two methods—one for increasing and another for decreasing—and, finally, a computed property to format the final data:
<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';

@Component
export default class Counter extends Vue {
valueNumber: number = 0;

get formattedNumber() {
return `Your total number is: ${this.valueNumber}`;
}

increase() {
this.valueNumber += 1;
}

decrease() {
this.valueNumber -= 1;
}
}
</script>
  1. It's time to create the template and rendering for this component. The process is the same as a JavaScript Vue file. We will add the buttons for increasing and decreasing the value and showing the formatted text:
<template>
<div>
<fieldset>
<legend>{{ formattedNumber }}</legend>
<button @click="increase">Increase</button>
<button @click="decrease">Decrease</button>
</fieldset>
</div>
</template>
  1. In the App.vue file, we need to import the component we just created:
<template>
<div id="app">
<Counter />
</div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Counter from './components/Counter.vue';

@Component({
components: {
Counter,
},
})
export default class App extends Vue {

}
</script>
<style lang="stylus">
#app
font-family 'Avenir', Helvetica, Arial, sans-serif
-webkit-font-smoothing antialiased
-moz-osx-font-smoothing grayscale
text-align center
color #2c3e50
margin-top 60px
</style>
  1. Now, when you run the npm run serve command on Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows), you will see your component running and executing on screen:

How it works...

The vue-class-component plugin makes use of the new proposal of decorators to inject and pass some attributes to the classes on TypeScript.

This injection helps in the process of simplifying the development of a component with a syntax more aligned with TypeScript than with the Vue common object.

See also

Find more information about vue-class-component at https://github.com/vuejs/vue-class-component.

主站蜘蛛池模板: 新龙县| 梅州市| 罗甸县| 大名县| 蓝田县| 海口市| 黄平县| 五华县| 安丘市| 资中县| 逊克县| 梁河县| 宜丰县| 白河县| 凉山| 西平县| 赞皇县| 永城市| 左贡县| 班玛县| 刚察县| 襄垣县| 马鞍山市| 澄迈县| 德钦县| 深水埗区| 灵武市| 四川省| 宿州市| 普安县| 永年县| 商河县| 历史| 旅游| 广汉市| 高唐县| 吴旗县| 扎兰屯市| 马山县| 湖北省| 台前县|