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. DedeCMS列表页隔行/多行随意换色

    在很多列表调用的时候都需要有隔行换色或者多行不同颜色,特别在全通式的首页轮展图的时候,要想实现轮展图背景随着图片的更换,超过三张或多张的时候,隔行换色已经不能解决问题了,在原来的隔行换色的基础上,进行 ...

  2. 微信、QQ中app的下载问题

    最近在做一个项目,有一项功能是从微信中的分享页或者产品推广页面中下载app:在微信中直接下载app时微信是“拒绝”的,所以一般的做法是点击下载按钮弹出遮罩层,提示在浏览器中打开,然后进入外部浏览器,再 ...

  3. 【TC SRM 718 DIV 2 B】Reconstruct Graph

    [Link]: [Description] 给你两个括号序列; 让你把这两个括号序列合并起来 (得按顺序合并) 使得组成的新的序列为合法序列; 即每个括号都能匹配; 问有多少种合并的方法; [Solu ...

  4. EditPlus,UltraEdit等编辑器列选择的方法

    在使用富文本编辑器的时候,通常模式是行选择状态,由于今天想使用EditPlus列选择状态, 于是通过在网上收集的资料,总结出相关富文本编辑器的列选择的方法. EditPlus  1)菜单:编辑 -&g ...

  5. 利用"SQL"语句自动生成序号的两种方式

    1.首先,我们来介绍第一种方式: ◆查询的SQL语句如下: select row_number() over (order by name) as rowid, sysobjects.[name] f ...

  6. atitit.jndi的架构与原理以及资源配置and单元測试实践

    atitit.jndi的架构与原理以及资源配置and单元測试实践 1. jndi架构 1 2. jndi实现原理 3 3. jndi资源配置 3 3.1. resin  <database> ...

  7. js---12对象创建方式,构造器,原型

    <script type="text/javascript"> var o = {}; var o1 = new Object();//这2种方式创建对象是一样的,因为 ...

  8. spring源码分析之@Conditional

    根源在AnnotationConfigApplicationContext和AnnotationConfigWebApplicationContext,以AnnotationConfigApplica ...

  9. Glide二次封装库的使用

    更多代码可以查询本人GitHub:欢迎阅读,star点起来. Glide二次封装库源码 前言 为什么选择Glide? Glide 轻量级 速度快 可以根据所需加载图片的大小自动适配所需分辨率的图 支持 ...

  10. SQL insert 主键冲突

    待总结 https://blog.csdn.net/JavaCoder_juejue/article/details/82313891 https://blog.csdn.net/a772304419 ...