Starter app: https://github.com/alexjoverm/Vue-Typescript-Starter

Writing Vue components as plain objects has very limited capabilities for TypeScript’s IntelliSense. This lesson will show you how to write components as classes to take full potential of TypeScript static typing by using vue-class-component.

Install:

npm install --save vue-class-component

Original file:

<template>
<div class="hello">
<h1>{{ message }}</h1>
<button @click="clicked">Click</button>
</div>
</template> <script lang="ts">
export default {
data () {
return {
message: 'Welcome to Your Vue.js App'
}
}, computed: {
fullMessage(){
return `${this.message} from Typescript`;
}
}, created() {
console.log('created');
}, methods: {
clicked(){
console.log('clicked');
}
}
}
</script>
  • Everything inside "data" mapping to props in class.
  • Everything inside "computed" mapping to get method
  • "created" lifecycle hook is just a function
  • Everything inside "methods" are also just functions.
<template>
<div class="hello">
<h1>{{ message }}</h1>
<button @click="clicked">Click</button>
</div>
</template> <script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component' @Component({})
export default class Hello extends Vue {
message: string = 'Welcome to Your Vue.js App' get fullMessage() {
return `${this.message} from Typescript`
} created() {
console.log('created');
} clicked(){
console.log('clicked');
}
} /*
export default {
data () {
return {
message: 'Welcome to Your Vue.js App'
}
}, computed: {
fullMessage(){
return `${this.message} from Typescript`;
}
}, created() {
console.log('created');
}, methods: {
clicked(){
console.log('clicked');
}
}
}*/
</script>

[Vue + TS] Write a Vue Component as a Class in TypeScript的更多相关文章

  1. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...

  2. 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践

    1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择  ...

  3. "[Vue warn]: Failed to mount component: template or render function not defined"错误的解决

    VUE中动态路由出错: vue.esm.js?a026: [Vue warn]: Failed to mount component: template or render function not ...

  4. vue +ts 在router的路由中import报错的解决方案

    在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...

  5. Vue官方文档Vue.extend、Vue.component、createElement、$attrs/$listeners、插槽的深入理解

    一.Vue.extend({}). 看官网文档介绍,Vue.extend({})返回一个Vue的子类,那么这个Vue子类是啥玩意儿呢?我直观感觉它就是创建出一个组件而已啊,那么它又和Vue.compo ...

  6. Vue报错之"[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead......"

    一.报错截图 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the p ...

  7. Vue报错之" [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component correctly? For recursive components, make sure to provide the "name" option."

    一.报错截图 [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component corre ...

  8. [Vue warn]: Failed to mount component: template or render function not defined. found in ---> <XFbwz> at src/views/XFbwz.vue <App> at src/App.vue <Root>

    1.引入.vue文件忘记加.vue 2.引入文件内容为空

  9. Vue 爬坑之路(十)—— Vue2.5 + Typescript 构建项目

    Typescript 在前端圈已经逐渐普及,Vue 2.5.0 改进了类型声明,使得对 TypeScript 更加友好 不过要想在项目中直接使用 TypeScript  仍然需要对项目进行一些改造 P ...

随机推荐

  1. 洛谷——P1518 两只塔姆沃斯牛 The Tamworth Two

    https://www.luogu.org/problem/show?pid=1518 题目背景 题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为( ...

  2. Linux监控(OS,MySQL,Tomcat,Apache)

    关于逐步脱离开发岗位的意见,老大已经批准了,接下来我的主要工作就是"运维+数据库管理".感谢杰民兄和小马哥能接受我的骚扰.接下来还会去骚扰他们,同一时候也会去骚扰董大爷,小刚总,心 ...

  3. Codeforces Round #262 (Div. 2) 题解

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. PHP CLI模式下的多进程应用分析

    PHP在非常多时候不适合做常驻的SHELL进程, 他没有专门的gc例程, 也没有有效的内存管理途径. 所以假设用PHP做常驻SHELL, 你会常常被内存耗尽导致abort而unhappy 并且, 假设 ...

  5. 设计模式之Build(生成者模式)

    一.生成器模式的定义: 生成器模式也称为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示(GoF).在软件设计中,有时候面临着一个非常复杂的对象的创 ...

  6. JSP从选择到放弃

    JSP为什么会出现?出现的原因? ①因为在开发web网站时候,返现servlet做界面比较麻烦,于是出现了jsp. 运行在服务端(Java server pages)基于servlet,对servle ...

  7. BZOJ5027: 数学题

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 140  Solved: 48[Submit][Status][Discuss] Description ...

  8. Css学习总结(1)——20个很有用的CSS技巧

    1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate { filter: grayscale(100%); -webkit-filter: graysca ...

  9. ANSI-X99MAC算法和PBOC的3DES MAC算法

    仅仅要有标准的DES加密和解密算法.类似ANSI-X99MAC算法和PBOC3DES算法就非常好实现.他们都是用DES算法再经过一层算法实现的.实现原理看图就能看明确.3DES算法实现就更简单了.就是 ...

  10. Makefile 文件格式

    Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o  code.o gcc  -o  test   prog.o   code.o 当中 ...