[Vue + TS] Write a Vue Component as a Class in TypeScript
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的更多相关文章
- [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 ...
- 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践
1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择 ...
- "[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 ...
- vue +ts 在router的路由中import报错的解决方案
在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...
- Vue官方文档Vue.extend、Vue.component、createElement、$attrs/$listeners、插槽的深入理解
一.Vue.extend({}). 看官网文档介绍,Vue.extend({})返回一个Vue的子类,那么这个Vue子类是啥玩意儿呢?我直观感觉它就是创建出一个组件而已啊,那么它又和Vue.compo ...
- 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 ...
- 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 ...
- [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.引入文件内容为空
- Vue 爬坑之路(十)—— Vue2.5 + Typescript 构建项目
Typescript 在前端圈已经逐渐普及,Vue 2.5.0 改进了类型声明,使得对 TypeScript 更加友好 不过要想在项目中直接使用 TypeScript 仍然需要对项目进行一些改造 P ...
随机推荐
- 进阶《Python高性能编程》中文PDF+英文PDF+源代码
入门使用高性能 Python,建议参考<Python高性能编程>,例子给的很多,讲到高性能就会提到性能监控,里面有cpu mem 方法的度量,网络讲了一点异步,net profiler 没 ...
- QQ空间说说爬虫
QQ空间说说爬虫 闲来无事,写了一个QQ空间的爬虫,主要是爬取以前的说说,然后生成词云. 这次采用的主要模块是selenium,这是一个模拟浏览器的模块,一开始我不想用这个模块写的,但是后面分析的时候 ...
- 紫书 例题 9-3 UVa 1347 ( 状态设计)
首先做一个转化,这种转化很常见. 题目里面讲要来回走一遍,所以就转化成两个从起点到终点,路径不重合 那么很容易想到用f[i][j]表示第一个走到i,第二个人走到j还需要走的距离 但是这里无法保证路径不 ...
- C++ Primer笔记12_运算符重载_递增递减运算符_成员訪问运算符
1.递增递减运算符 C++语言并不要求递增递减运算符必须是类的成员.可是由于他们改变的正好是所操作对象的状态.所以建议设定为成员函数. 对于递增与递减运算符来说,有前置与后置两个版本号,因此.我们应该 ...
- KendoUi中KendoDropDownList控件的使用——三级级联模块的实现
1. 应用需求 在权限系统开发中除了以上数据表关系的设计之外.比較麻烦的地方是级联模块在页面的展示,因为设计中最多是控制到三级,因此三级级联模块的展示.编辑等页面操作是须要解决的问题,这里採用Kend ...
- C#结构函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Cisco交换机SPAN&RSPAN调试实录
Cisco交换机SPAN&RSPAN设置实录 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处http://chenguang.blog.51cto.com/3 ...
- Cisco Works 2000 网络管理软件安装、配置全过程
下面是在windows 2000 server 下安装ciscoworks 2000的过程.(附件中是标准avi格式文件,由于上传附件大小限制,更多内容见Sina播客) 浏览全部原创视频请见: htt ...
- Linux系统下到哪儿寻找硬件错误
Linux系统下到哪儿寻找硬件错误 当linux系统出现故障的时候,作为管理员首先要定位错误,现在linux有许多工具都能帮助用户寻找错误,要学会利用他们确定问题.这些工具包括dmesg. ...
- LuoguP2763 试题库问题(最大流)
建图同_____ 代码: #include<queue> #include<cstdio> #include<cstring> #include<algori ...