type script
简介
- 微软推出的静态类型的语言
- 特点:强类型、遵循ES6、编译器严谨的语法提示
- 难度从大到小:ts>ES6>javascript
- 运行:运行的时候,ts编译为js
- vue、angular等众多公司推荐使用
定义变量
字符串
const str = ref<string>("abc")
const str = ref("abc")
根据值类型自动判断改类型变量是什么
数字
const count = ref<number>(10)
const count = ref(10)
布尔
const flag = ref<bollean>(true)
const flag = ref((true)
函数
function add(n1:string,n2:number):void{}
接口
定义接口
interface Iuser = {
name:string,
age:number|string
}
const user = reactive<Iuser>({
name:"chenchen",
age:18
})
在setup中获取props
导入
import{defineProps} from 'vue'
interface Iprops = {min:number,max:number}
声明
const props = defineProps<Iprops>()
使用ts构建组件
数字加减
<template>
<span>
<button :disabled="count<=props.min" @click="countChange(-1)">-</button>
<input type="text" v-model.number="count">
<button :disabled="count>=props.max" @click="countChange(1)">+</button>
</span>
</template>
<script lang="ts" setup>
import {defineProps,ref} from 'vue';
interface Iprops {
max:number,
min:number,
step:number,
value:number,
}
const props = defineProps<Iprops>()
const count = ref(props.value);
function countChange(n:number,val:number):void{
count.value+=props.step*n;
}
</script>
效果: