vue3中的TS

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

<script lang="ts" setup>
import type{ Ref } from 'vue'
import { ref } from 'vue'
const count = ref(1)
 const c:Ref<number> = count
</script>

<template>
  <div>{{ c.toFixed(2) }}</div>
</template>

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

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

import { defineComponent } from "vue"
export default defineComponent({
  props: {
    message: {
      type: String,
      required: true
    }
  },

  setup(props) {
    let x: String = props.message
      console.log(x);
  }

})

3.断言as

<script lang="ts" setup>
let x: string | number = 1

</script>

<template>
  <div>{{ (x as number).toFixed(2) }}</div>
</template>

<style lang="css">
/* css 代码 */
</style>

4.在setup中的props类型

(1)通过对象

<script lang="ts" setup>
  const props = defineProps({
    bar: Number
  })

  const b: Number = props.bar
</script>

(2)通过泛型

<script lang="ts" setup>
// 基于类型的声明
const props = defineProps<{
  foo: string,
  bar: number
}>()

let f: string = props.foo

(3)通过接口

<script lang="ts" setup>
interface Props {
  foo: string,
  bar: number
}
const props = defineProps<Props>()
let f: string = props.foo

</script>

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

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

emit

defineEmits

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

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

ref类型

通过泛型

<script lang="ts" setup>
import { ref } from 'vue'
import type { Ref } from 'vue'

const year: Ref<string | number> = ref('2022') //在里面可以写字符串也可以写数字了
year.value = 2020
</script>

reactive类型

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

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

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

computed类型

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

对象中事件函数的定义

<script lang="ts" setup>
function handleChange(event: Event) {
  console.log((event.target as HTMLInputElement).value); //通过断言event.target是什么类型
  return 2
}

function handleClick(event: Event) {
  console.log((event.target as HTMLButtonElement).innerHTML)
}
</script>

<template>
  <input type="text" @change="handleChange">
  <!-- <input type="button" @click="handleClick"> -->
  <button @click="handleClick">aa</button>
</template>

provide  inject类型

知识点:InjectionKey

import type { InjectionKey } from 'vue'


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

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

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

绑定元素的ref类型

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
const ipt = ref<HTMLInputElement | null>(null)//定义ipt是input或者null类型
onMounted(() => {
  ipt.value?.focus()
})
</script>
<template>
  <input type="text" ref="ipt">
</template>
<style lang="css"> 
/* css 代码 */
</style>

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 使用 TypeScript 编写 CherryMarkdown 可以按照以下步骤进行: 1. 安装 CherryMarkdown ```bash npm install cherry-markdown ``` 2. 在 Vue 单文件组件使用 CherryMarkdown ```vue <template> <div> <cherry-markdown :source="markdownContent" /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import CherryMarkdown from 'cherry-markdown'; export default defineComponent({ components: { CherryMarkdown }, data() { return { markdownContent: '# Hello, CherryMarkdown!' }; } }); </script> ``` 在 TypeScript ,需要使用 `defineComponent` 函数来定义 Vue 组件,并显式地指定 `CherryMarkdown` 组件。 3. 在 TypeScript 使用 CherryMarkdown 的 Props CherryMarkdown 提供了多个 Props 用于控制渲染行为,例如 `source` 用于指定源 Markdown 文本,`theme` 用于指定渲染主题等。在 TypeScript 使用 Props,需要先定义 Props 的类型: ```ts interface CherryMarkdownProps { source: string; theme?: string; // 其他 Props } ``` 然后在组件显式地声明 Props: ```ts import { defineComponent } from 'vue'; import CherryMarkdown from 'cherry-markdown'; interface CherryMarkdownProps { source: string; theme?: string; // 其他 Props } export default defineComponent({ components: { CherryMarkdown }, props: { source: { type: String, required: true }, theme: { type: String, default: 'default' }, // 其他 Props }, setup(props: CherryMarkdownProps) { // 在 setup 函数可以使用 props // ... } }); ``` 这样就可以在 TypeScript 使用 CherryMarkdown 的 Props 了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值