脚本宝典收集整理的这篇文章主要介绍了Vue3 + TypeScript 开发总结,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
npm install -g @vue/cli yarn global add @vue/cli
1.3 现有Vue 2 项目 升级到 Vue3
vue add typescript
【在Vue3 中 编写组合函数,使用 ComposITon Api SETUP 来解决】
在Vue3中,也可以不使用 Composition Api
来编写组件,它只是在Vue3 中编写组件中的另一种方法,内部简化了好多操作。
所以你还可以继续使用 Vue2 的方式来 编写 组件。
TypeScript` 的支持
编写大型组件时,可以使用 Composition Api
组合函数很好的管理状态
跨组件重用代码时
4.1 什么是 setup
setup 是用来配置组件状态的另一种实现。
在setup 中定义的状态,方法要想在模板中使用,必须 return
注意:
在 Vue2 中,我们定义一个响应式变量可以直接在 data 中 定义并且在模板中使用该变量。 如果 使用的 composition api 的话,我们得在 setup 中 使用 ref 来创建 响应式变量,并且得将它返回,才能在页面中使用。
使用:
这样的好处:
<template> <div> <h1>{{title}}</h1> </div> </template> <script> import {ref,defineComponent} from 'vue' export default defineComponent({ setup () { // 定义响应式变量 const title = ref('前端自学社区') // 访问该变量 console.LOG(title.value) // 返回变量 return {title} } }) </script>
Composition Api 生命周期钩子 和 Vue 2 选项式 生命周期 钩子名称一样,只是在使用 组合式API 时,前缀为 on , onmounted`
下面代码中有两个 mounted 生命钩子,你猜哪个会先执行?
setup
会先执行
setup () { // 定义响应式变量 const title = ref('前端自学社区') console.log(title) // 返回变量 function getTitle(){ console.log(title.value) } // 页面在加载 onMounted(getTitle) return {title} }, mounted() { console.log('测试 mounted 执行顺序') },
在 setup 中使用 watch响应式更改
1.引入 watch, import { watch } from 'vue'
2.直接使用watch,watch 接受 3 个参数
import {wathc} from 'vue' // 定义响应式变量 const num = ref(0) // 更新响应式变量 function changeNum(){ num.value++ } // wathc 监听响应式变量 watch( num,(newValue, oldValue) => { console.log(`newValue为:${newValue},--------oldValue为:${oldValue}`) } )
它也是 从 vue 导入,computed 函数返回一个作为 computed 的第一个参数传递的 getter 类回调的输出的一个只读的响应式引用。为了访问新创建的计算变量的 value,我们需要像使用 ref 一样使用 .value property。
当num值变化时,nums 的值会 *3
import {ref,computed} from 'vue'; const num = ref(0) //更新num function changeNum(){ num.value++ } //监听num变化 const nums = computed(() =>{ return num.value * 3 })
props : 父组件传递过来的属性, setup` 函数中 props 是响应式的,它会随着数据更新而更新,并且不能使用 ES6 解构,因为它会不能使 props 为响应式。
context : 它是一个普通的 对象,它暴露3个组件的· property
context
不是 响应式的,所以可以使用ES6
解构来简便写法。
props:{ obj:{ type:Object } }, setup (props,{attrs,slots,emit}) { console.log(attrs) console.log(slots) console.log(emit) console.log(props.obj) }
5.2 组件加载 setup
时注意
在组件执行 setup
时, 组件实例没有被创建,因此就无法访问以下属性
在 setup
中使用 生命周期时,前缀必须加 on.
在 Vue 2
中,我们可以使用 Provide/Inject
跨组件传值,在 Vue 3 中也可以。
在 setup
中 使用,必须从 vue
中导入使用。
使用 Provide
时,一般设置为 响应式更新的,这样的话,父组件变更,子组件,子孙组件也跟着更新。
怎么设置为响应式更新呢?
父组件:
import { provide, defineComponent, ref, reactive } from "vue"; <template> <Son/> </template> <script> import { provide, defineComponent, ref, reactive } from "vue"; export default defineComponent({ setup() { const father = ref("我父组件"); const info = reactive({ id: 23, message: "前端自学社区", }); function changeProvide(){ info.message = '测试' } provide('father',father) provide('info',info) return {changeProvide}; } }) </script>
子组件:
<template> <div> <h1>{{info.message}}</h1> <h1>{{fatherData}}</h1> </div> </template> <script> import {provide, defineComponent,ref,reactive, inject} from 'vue' export default defineComponent({ setup () { const fatherData = inject('father') const info = inject('info') return {fatherData,info} } }) </script>
采用 TypeScirpt
的特性, 类型断言 + 接口 完美的对 属性进行了 约束
interface
分页查询 字段属性类型验证:
export default interface queryType{ page: Number, Size: Number, name: String, age: Number }
组件中使用:
import queryType from '../interface/Home' data() { return { query:{ page:0, size:10, name:'测试', age: 2 } as queryType } },
这样 TypeScript 正确推断 Vue 组件选项中的类型
import { defineComponent } from 'vue' export default defineComponent({ setup(){ return{ } } })
8.3 类型声明 reactive
export default interface Product { name:String, price:Number, address:String } import Product from '@/interface/Product' import {reactive} from 'vue' const product = reactive({name:'xiaomi 11',price:5999,address:'北京'}) as Product return {fatherData,info,product}
以上就是Vue3 + TypeScript 开发总结的详细内容,更多关于Vue3 + TypeScript的资料请关注脚本宝典其它相关文章!
以上是脚本宝典为你收集整理的Vue3 + TypeScript 开发总结全部内容,希望文章能够帮你解决Vue3 + TypeScript 开发总结所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。