• vue3中的TS


    1.当导入导出类型模块的时候,需要加一个type,并且在vite框架下,不允许使用枚举类型

    1. <script lang="ts" setup>
    2. import type{ Ref } from 'vue'
    3. import { ref } from 'vue'
    4. const count = ref(1)
    5. const c:Ref<number> = count
    6. </script>
    7. <template>
    8. <div>{{ c.toFixed(2) }}</div>
    9. </template>

    2.defineComponent让属性具有ts的特性

    defineComponent可以给组件的setup方法准确的参数类型定义.
    defineComponent 可以接受显式的自定义 props 接口或从属性验证对象中自动推断
    defineComponent 可以正确适配无 props、数组 props 等形式
    引入 defineComponent() 以正确推断 setup() 组件的参数类型
     

    1. import { defineComponent } from "vue"
    2. export default defineComponent({
    3. props: {
    4. message: {
    5. type: String,
    6. required: true
    7. }
    8. },
    9. setup(props) {
    10. let x: String = props.message
    11. console.log(x);
    12. }
    13. })

    3.断言as

    1. <script lang="ts" setup>
    2. let x: string | number = 1
    3. </script>
    4. <template>
    5. <div>{{ (x as number).toFixed(2) }}</div>
    6. </template>
    7. <style lang="css">
    8. /* css 代码 */
    9. </style>

    4.在setup中的props类型

    (1)通过对象

    1. <script lang="ts" setup>
    2. const props = defineProps({
    3. bar: Number
    4. })
    5. const b: Number = props.bar
    6. </script>

    (2)通过泛型

    1. <script lang="ts" setup>
    2. // 基于类型的声明
    3. const props = defineProps<{
    4. foo: string,
    5. bar: number
    6. }>()
    7. let f: string = props.foo

    (3)通过接口

    1. <script lang="ts" setup>
    2. interface Props {
    3. foo: string,
    4. bar: number
    5. }
    6. const props = defineProps<Props>()
    7. let f: string = props.foo
    8. </script>

    (4)?:当不传的时候可以给个默认值

    1. <script lang="ts" setup>
    2. interface Props {
    3. foo: string;
    4. bar?: number //可以不传,传的话就是number
    5. }
    6. const { foo, bar = 10 } = defineProps<Props>() //不传的话给个默认值10
    7. </script>

    emit

    defineEmits

    通过泛型定义emit出去的函数的事件名,参数以及返回值

    1. <script lang="ts" setup>
    2. const emit = defineEmits<{ //分别抛出是两个函数
    3. (e: 'change', id: number) : number //change是事件名,id是参数,后面:number是函数返回的类型
    4. (e: 'updata', value: string): void
    5. }>()
    6. </script>

    ref类型

    通过泛型

    1. <script lang="ts" setup>
    2. import { ref } from 'vue'
    3. import type { Ref } from 'vue'
    4. const year: Ref<string | number> = ref('2022') //在里面可以写字符串也可以写数字了
    5. year.value = 2020
    6. </script>

    reactive类型

    1. <script lang="ts" setup>
    2. import { reactive } from 'vue'
    3. const list = reactive({id: 100, name: 'felixlu'})
    4. const id: number = list.id
    5. const n: string = list.name
    6. </script>

    通过接口定义reactive里面对象的类型

    1. <script lang="ts" setup>
    2. import { reactive } from 'vue'
    3. interface List {
    4. id: number,
    5. name: string
    6. }
    7. const list1: List = reactive({id: 100, name: 'felixlu'}) //对象里面的类型必须与接口里面的类型是一致的
    8. </script>

    computed类型

    1. import { ref, computed } from 'vue'
    2. import type { ComputedRef } from 'vue'
    3. const count = ref(100)
    4. // addOne: ComputedRef<number>
    5. const addOne = computed <number>(() => count.value + 1)
    6. const a: ComputedRef<number> = addOne
    7. a.value.toFixed(1)
    8. // a.value.indexOf() //数字没有indexof方法
    9. </script>

    对象中事件函数的定义

    1. <script lang="ts" setup>
    2. function handleChange(event: Event) {
    3. console.log((event.target as HTMLInputElement).value); //通过断言event.target是什么类型
    4. return 2
    5. }
    6. function handleClick(event: Event) {
    7. console.log((event.target as HTMLButtonElement).innerHTML)
    8. }
    9. </script>
    10. <template>
    11. <input type="text" @change="handleChange">
    12. <!-- <input type="button" @click="handleClick"> -->
    13. <button @click="handleClick">aa</button>
    14. </template>

    provide  inject类型

    知识点:InjectionKey

    import type { InjectionKey } from 'vue'

    1. <script lang="ts" setup>
    2. import { provide, inject,ref } from 'vue'
    3. import type { InjectionKey,Ref } from 'vue'
    4. const key1=Symbol() as InjectionKey<number>//首先断言是InjectionKey 然后标记是number类型
    5. let key2 = Symbol() as InjectionKey <Ref<string>> //因为下面传的是ref类型
    6. const X=ref('100')
    7. provide(key1, 2)
    8. provide(key2,X )
    9. const fee:number|undefined =inject(key1)
    10. const foo:Ref<string>|undefined = inject<Ref<string>>(key2,ref('我是默认值')) //inject后面的类型是默认值的类型
    11. </script>

    重点:当通过provide注入对象的写法

    1. import { provide, inject,reactive} from 'vue'
    2. import type { InjectionKey } from 'vue'
    3. //interface List {
    4. //id: number,
    5. //name: string
    6. //}这种对象的接口只能对对象进行查改
    7. interface List {
    8. [key:string]:number|string|boolean
    9. } //可以实现对象的增删查改
    10. const list1 = reactive({id: 100, name: 'felixlu'})
    11. const key1=Symbol() as InjectionKey<List>
    12. provide(key1, list1) //注入
    13. const fee:List|undefined =inject(key1)//接收

    绑定元素的ref类型

    1. <script lang="ts" setup>
    2. import { ref, onMounted } from 'vue'
    3. const ipt = ref<HTMLInputElement | null>(null)//定义ipt是input或者null类型
    4. onMounted(() => {
    5. ipt.value?.focus()
    6. })
    7. </script>
    8. <template>
    9. <input type="text" ref="ipt">
    10. </template>
    11. <style lang="css">
    12. /* css 代码 */
    13. </style>

  • 相关阅读:
    Python的pip包管理器介绍和使用
    张丽俊:穿透不确定性要靠四个“不变”
    公司注册地址要注意,小心被拉“黑”,征信受影响
    【深度学习】多标签学习
    深入了解JVM之⭐《优化JIT》⭐
    PayPal VS Block:开启全球金融科技的新未来
    MinIO对象生命周期管理解析
    代码源每日一题div2 可重排列
    深度学习 -- pytorch 计算图与动态图机制 autograd与逻辑回归模型
    【计算机网络】网络层IP协议
  • 原文地址:https://blog.csdn.net/m0_66195841/article/details/125434318