什么是Typescript

TypeScript 是一种由微软开发的自由和开源的编程语言,它是 JavaScript 的一个超集,扩展了 JavaScript 的语法。作者是安德斯大爷, Delphi、C# 之父(你大爷永远是你大爷)。把弱类型语言改成了强类型语言,拥有了静态类型安全检查, IDE 智能提示和追踪,代码重构简单、可读性强等特点。

现在VUE 也支持了 TypeScript ,面对安德斯大爷放出的这些大招,果断用之。

安装使用

使用 vue-cli 创建项目的时候 选择Typescript就行了,

注意下几个配置文件



tsconfig.json

{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}

tslint.json

{
"defaultSeverity": "warning",
"extends": [
"tslint:recommended"
],
"linterOptions": {
"exclude": [
"node_modules/**"
]
},
"rules": {
"quotemark": [true, "single"],
"indent": [true, "spaces", 2],
"interface-name": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"no-consecutive-blank-lines": false,
"no-console": false, //允许使用console
"member-access": [true, "no-public"], //禁止指定公共可访问性,因为这是默认值
// "noImplicitAny": false, //允许参数而不声明其类型
"one-variable-per-declaration": false, //允许在同一声明语句中使用多个变量定义
"no-unused-expression": [true, "allow-fast-null-checks"], //允许使用逻辑运算符执行快速空检查并执行副作用的方法或函数调用( 例如e && e.preventDefault())
"curly": [true, "ignore-same-line"],
"arrow-parens": [true, "ban-single-arg-parens"],
"semicolon": [true, "never"],//是否提示不必要的分号
"trailing-comma": [
true,
{
"multiline": {
"objects": "ignore",
"arrays": "ignore",
"functions": "ignore",
"typeLiterals": "ignore"
},
"esSpecCompliant": true
}
]
}
}

重要的是怎么在项目中使用Typescrit写法

1:安装npm install --save vue-property-decorator

此类库提供了7个装饰器

  • @Emit
  • @Inject
  • @Model
  • @Prop
  • @Provide
  • @Watch
  • @Component

    实现生成像原生 JavaScript class 那样的声明组件。

下面分别给出实例解释其用法:

  • @Component

    组件声明

    原生写法
import UploadImage from '@/components/UploadImage'

export default {
name: 'user',
components: { UploadImage },
data() {
return {
name:"张三",
sex: '男'
}
},
methods: {
funcA(params) {},
funcB() {}
}
}

使用Ts中写法

import UploadImage from '@/components/UploadImage'
import { Component, Vue, Provide } from 'vue-property-decorator' @Component(name:"user",components:{UploadImage})
export default class user extends Vue{
private name:string="张三"
private sex:string="男" private funcA(params:any){}
private funcB(){}
}

其中使用 @Component 声明了 user组件 ,同时引用 子组件 UploadImage,写在 Components 参数中。

  • @Prop

    属性声明 在自定义组建中使用

    原生写法
export default{
name:"upload",
props:{
value:{
type:String,
default:''
}
}
}

在ts中写法

@Component()
export default class upload extends Vue{
@Prop()
private value:string='';
}
  • computed

    计算属性

    这个很类似于c#中的 属性概念,属性值本身可以通过计算得出。

原生写法

computed: {
imageUrl() {
return 'http://xxxx.xxxx.com/' + this.value;//value是定义的一个字段
}
},

在ts中写法

get imageUrl(){
return 'http://xxxx.xxxx.com/' + this.value;//value是定义的一个字段
} template 中一样使用{{imageUrl}}
  • @watch

    用来监测Vue实例上的数据变动

    如果对应一个对象,键是观察表达式,值是对应回调,值也可以是方法名,或者是对象,包含选项。
  export default {
name: 'index',
data() {
return {
demo: {
name: ''
},
value: ''
};
},
computed: {
newName() {
return this.demo.name;
}
},
watch: {
newName(val) {
this.value = val;
}
}
};

ts写法

export default class index extends Vue{
demo:any={name:''};
value:string=''; get newName(){
return this.demo.name;
} @watch('wnewName')
wnewName(val){
this.value=val;
}
}
  • emit

    我们知道,父组件是使用 props 传递数据给子组件,但如果子组件要把数据传递回去,应该怎样做?那就是自定义事件!

每个 Vue 实例都实现了事件接口(Events interface),即:

-   使用 $on(eventName) 监听事件
- 使用 $emit(eventName)触发事件
Vue.component('counter', {
template: `
<button v-on:click="increment">{{ counter }}</button>`,
data() {
return {
counter: 0
}
},
methods: {
increment: function () {
this.counter += 1
this.$emit('increment')
}
},
}); new Vue({
el: '#example',
data: {
total: 0
},
methods: {
incrementTotal: function () {
this.total += 1
}
}
}) <div id="example">
<p>{{ total }}</p>
<counter v-on:increment="incrementTotal"></counter>
</div>

子组件自定义了个事件,然后把这个事件发射出去,父组件使用这个事件

vue-property-decorator vue typescript写法的更多相关文章

  1. [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript

    Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as for ...

  2. 用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象

    用TypeScript开发Vue--如何通过vue实例化对象访问实际ViewModel对象 背景 我个人很喜欢TypeScript也很喜欢Vue,但在两者共同使用的时候遇到一个问题. Vue的实例化对 ...

  3. 如何在Vue项目中使用Typescript

    0.前言 本快速入门指南将会教你如何在Vue项目中使用TypeScript进行开发.本指南非常灵活,它可以将TypeScript集成到现有的Vue项目中任何一个阶段. 1.初始化项目 首先,创建一个新 ...

  4. Property 'validate' does not exist on type 'Element | Element[] | Vue | Vue[]'. Property 'valid...

    使用vue-cli 3.0+Element-ui时候,调用form表单校验时候出现的问题是: Property 'validate' does not exist on type 'Element | ...

  5. vue cli4构建基于typescript的vue组件并发布到npm

    基于vue cli创建一个vue项目 首先安装最新的vue cli脚手架, npm install --global @vue/cli npm WARN optional SKIPPING OPTIO ...

  6. Vue项目中应用TypeScript

    一.前言 与如何在React项目中应用TypeScript类似 在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator, 其是基于vue-class-c ...

  7. 使用@vue/cli搭建vue项目开发环境

    当前系统版本 mac OS 10.14.2 1.安装node.js开发环境 前端开发框架和环境都是需要 Node.js  vue的运行是要依赖于node的npm的管理工具来实现 <mac OS ...

  8. Vue学习笔记-Vue.js-2.X 学习(一)===>基本知识学习

    一  使用环境: windows 7 64位操作系统 二  IDE:VSCode/PyCharm 三  Vue.js官网: https://cn.vuejs.org/ 四  下载安装引用 方式1:直接 ...

  9. 【Vue课堂】Vue.js 父子组件之间通信的十种方式

    这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ...

随机推荐

  1. collection介绍

    1.collection介绍 在mongodb中,collection相当于关系型数据库的表,但并不需提前创建,更不需要预先定义字段 db.collect1.save({username:'mayj' ...

  2. mui的app页面使用layui填充数据

    在mui的开发中有个坑,mui.plusReady在web上使用时是不会起作用的,只能在app上才行,所以推荐自己测试时使用mui.ready去写加载时的方法. 前端请求的返回格式为json,所以在后 ...

  3. Android实现多语言so easy

    微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521声明:本文由CodingAndroid原创,未经授权,不可随意转载! 最近,我们公 ...

  4. 什么时候使用redis?什么时候使用memcache?

    要清楚为什么,redis具有高可用特性,并且可固化,但特性有时候不能成为选择他的理由,一些业务场景中并不需要这样的特性.   什么时候倾向于选择redis? 1.复杂数据结构 value是哈希,列表, ...

  5. mysql row size上限

    mysql innodb 的 row size上限 背景 在项目使用中,出现了以下报错: Error Code: 1118 - Row size too large (> 8126). Chan ...

  6. zabbix自发现item监控

    在zabbix监控中,我们可以通过自带item的可以和自定义key进行监控,但是当所需要的监控项不确定,比如key会根据时间进行变化时,这时候我们就不能把item的key定义死,要通过自发现这个高级功 ...

  7. Map集合的遍历(利用entry接口的方式)

    核心思想: 调用map集合中的方法entrySet()将集合中的映射关系存放在Set集合中. 迭代Set集合 获取出的Set集合的元素是映射关系对象 通过映射关系对象方法的getKey(),getVa ...

  8. Python机器学习之数据探索可视化库yellowbrick

    # 背景介绍 从学sklearn时,除了算法的坎要过,还得学习matplotlib可视化,对我的实践应用而言,可视化更重要一些,然而matplotlib的易用性和美观性确实不敢恭维.陆续使用过plot ...

  9. RabbitMQ简洁安装

    在实际开发过程中,为了解决并发量大的问题,我们往往会引入消息中间件这个杀手锏,今天带大家先入门一个消息中间件RabbitMQ,我们会从RabbitMQ安装.使用来分享. 1. RabbitMQ安装 这 ...

  10. The used SELECT statements have a different number of columns???

    今天我们组就我一个人留守在这里修复bug了,有点小悲伤啊,他们都问我能不能hold得住啊,我当然能hold得住啊: 在看一个入库的存储过程中,在数据库运行的时候是没问题的,项目已启动,进行入库操作就是 ...