Vue3 + TypeScript 开发实践总结

/

Vue3 + TypeScript Study

一, 环境配置

1.1 安装最新 Vue 脚手架

  1. npm install -g @vue/cli
  2. yarn global add @vue/cli

1.2 创建Vue3 项目

  1. vue create projectName

1.3 现有Vue 2 项目 升级到 Vue3

  1. vue add typescript

二, 进击Vue3

2. 1 Vue 2 局限性

  1. 随着组件与组件依赖之间不断变大,组件很难读取和维护

  2. 没有完美的方法解决跨组件代码重用

2.2 Vue 3 如何解决Vue 2 局限

  1. 组件难以维护管理
    【在Vue3 中 编写组合函数,使用 Compositon Api setUp 来解决】

  2. 没有完美的方法解决跨组件代码重用

三,Vue3 Composition Ap i

3.1 关于 Composition Api

在Vue3中,也可以不使用 Composition Api 来编写组件,它只是在Vue3 中编写组件中的另一种方法,内部简化了好多操作。

所以你还可以继续使用 Vue2 的方式来 编写 组件。

3.2 什么时候使用Composition Api

  1. TypeScript` 的支持

  2. 编写大型组件时,可以使用 Composition Api 组合函数很好的管理状态

  3. 跨组件重用代码时

四,Composition Api 必备基础

4.1 什么是 setup

setup 是用来配置组件状态的另一种实现。

在setup 中定义的状态,方法要想在模板中使用,必须 return

注意:

  • setup 方法是在 components , props data Methods Computed Lifecycle methods 之前执行
  • 同时在 setup 中是不能访问 this

4.2 ref 创建响应式变量

在 Vue2 中,我们定义一个响应式变量可以直接在 data 中 定义并且在模板中使用该变量。 如果 使用的 composition api 的话,我们得在 setup 中 使用 ref 来创建 响应式变量,并且得将它返回,才能在页面中使用。

使用

  1. 引入 ref import { ref } from ‘vue’
  2. 初始变量 const name = ref(‘指定默认值’)
  3. 返回变量 return { name } 在return中还可以返回方法
  4. 在 setup 中 访问 定义的变量值,不能直接通过变量名来获取, 必须通过 变量名.value 来获取到该对象 、 值

这样的好处

  • 状态好管理,可以划分好几个 setup 状态管理,最后在一个 文件导入所有,并且使用。
  1. <template>
  2. <div>
  3. <h1>{{title}}</h1>
  4. </div>
  5. </template>
  6. <script>
  7. import {ref,defineComponent} from 'vue'
  8. export default defineComponent({
  9. setup () {
  10. // 定义响应式变量
  11. const title = ref('前端自学社区')
  12. // 访问该变量
  13. console.log(title.value)
  14. // 返回变量
  15. return {title}
  16. }
  17. })
  18. </script>

4.3 生命周期

Composition Api 生命周期钩子 和 Vue 2 选项式 生命周期钩子名称一样,只是在使用 组合式API时,前缀为 on, onMounted`

sd

下面代码中有两个 mounted 生命钩子,你猜哪个会先执行?

setup 会先执行

  1. setup () {
  2. // 定义响应式变量
  3. const title = ref('前端自学社区')
  4. console.log(title)
  5. // 返回变量
  6. function getTitle(){
  7. console.log(title.value)
  8. }
  9. // 页面在加载
  10. onMounted(getTitle)
  11. return {title}
  12. },
  13. mounted() {
  14. console.log('测试 mounted 执行顺序')
  15. },

4.4 watch

在 setup 中使用 watch响应式更改

  • 引入 watch, import { watch } from ‘vue’

  • 直接使用watch,watch 接受 3 个参数

  1. 要监听更新的 响应式引用或者 getter 函数
  2. 一个回调用来做更新后的操作
  3. 可选配置项
  1. import {wathc} from 'vue'
  2. // 定义响应式变量
  3. const num = ref(0)
  4. // 更新响应式变量
  5. function changeNum(){
  6. num.value++
  7. }
  8. // wathc 监听响应式变量
  9. watch(
  10. num,(newValue, oldValue) => {
  11. console.log(`newValue为:${newValue},--------oldValue为:${oldValue}`)
  12. }
  13. )

4.5 computed

它也是 从 vue 导入,computed 函数返回一个作为 computed 的第一个参数传递的 getter 类回调的输出的一个只读的响应式引用。为了访问新创建的计算变量的 value,我们需要像使用 ref 一样使用 .value property。

  • 当num值变化时,nums 的值会 *3
  1. import {ref,computed} from 'vue';
  2. const num = ref(0)
  3. //更新num
  4. function changeNum(){
  5. num.value++
  6. }
  7. //监听num变化
  8. const nums = computed(() =>{
  9. return num.value * 3
  10. })

五,setup

5.1 接受两个参数

props: 父组件传递过来的属性, setup 函数中 props 是响应式的,它会随着数据更新而更新,并且不能使用 ES6 解构,因为它会不能使 props 为响应式。

context : 它是一个普通的 对象,它暴露3个组件的· property

  1. Attribute
  2. 插槽
  3. 触发事件

context 不是 响应式的,所以可以使用ES6 解构来简便写法。

  1. props:{
  2. obj:{
  3. type:Object
  4. }
  5. },
  6. setup (props,{attrs,slots,emit}) {
  7. console.log(attrs)
  8. console.log(slots)
  9. console.log(emit)
  10. console.log(props.obj)
  11. }

5.2 组件加载 setup 时注意

在组件执行 setup 时, 组件实例没有被创建,因此就无法访问以下属性

  • data
  • computed
  • methods

    六,生命周期

    在 setup 中使用 生命周期时,前缀必须加 on.
选项式 API Hook inside setup
beforeCreate
created
beforeMount onBeforeMount
mounted onMounted
beforeUpdate onBeforeUpdate
updated onUpdated
beforeUnmount onBeforeUnmount
unmounted onUnmounted
errorCaptured onErrorCaptured
renderTracked onRenderTracked
renderTriggeredonRenderTriggered onRenderTriggered

七, 跨组件之间传值

在 Vue 2 中,我们可以使用 Provide/Inject 跨组件传值,在 Vue 3 中也可以。

在 setup 中 使用,必须从 vue 中导入使用。

使用 Provide 时,一般设置为 响应式更新的,这样的话,父组件变更,子组件,子孙组件也跟着更新。

怎么设置为响应式更新呢?

  1. 使用 ref / reactive 创建响应式变量
  2. 使用 provide(‘name’, ‘要传递的响应式变量’)
  3. 最后添加一个更新 响应式变量的事件,这样响应式变量更新, provide 中的变量也跟着更新。

父组件

  1. import { provide, defineComponent, ref, reactive } from "vue";
  2. <template>
  3. <Son/>
  4. </template>
  5. <script>
  6. import { provide, defineComponent, ref, reactive } from "vue";
  7. export default defineComponent({
  8. setup() {
  9. const father = ref("我父组件");
  10. const info = reactive({
  11. id: 23,
  12. message: "前端自学社区",
  13. });
  14. function changeProvide(){
  15. info.message = '测试'
  16. }
  17. provide('father',father)
  18. provide('info',info)
  19. return {changeProvide};
  20. }
  21. })
  22. </script>

子组件

  1. <template>
  2. <div>
  3. <h1>{{info.message}}</h1>
  4. <h1>{{fatherData}}</h1>
  5. </div>
  6. </template>
  7. <script>
  8. import {provide, defineComponent,ref,reactive, inject} from 'vue'
  9. export default defineComponent({
  10. setup () {
  11. const fatherData = inject('father')
  12. const info = inject('info')
  13. return {fatherData,info}
  14. }
  15. })
  16. </script>

八, 在Vue 中 使用 TypeScirpt 技巧

8.1 接口约束约束属性

采用 TypeScirpt 的特性, 类型断言 + 接口 完美的对 属性进行了 约束

interface

  1. //分页查询 字段属性类型验证
  2. export default interface queryType{
  3. page: Number,
  4. size: Number,
  5. name: String,
  6. age: Number
  7. }

组件中使用

  1. import queryType from '../interface/Home'
  2. data() {
  3. return {
  4. query:{
  5. page:0,
  6. size:10,
  7. name:'测试',
  8. age: 2
  9. } as queryType
  10. }
  11. },

8.2 组件使用 来 defineComponent 定义

这样 TypeScript 正确推断 Vue 组件选项中的类型

  1. import { defineComponent } from 'vue'
  2. export default defineComponent({
  3. setup(){
  4. return{ }
  5. }
  6. })

8.3 类型声明 reactive

  1. export default interface Product {
  2. name:String,
  3. price:Number,
  4. address:String
  5. }
  6. import Product from '@/interface/Product'
  7. import {reactive} from 'vue'
  8. const product = reactive({name:'xiaomi 11',price:5999,address:'北京'}) as Product
  9. return {fatherData,info,product}

转载请注明作者和出处,并添加本页链接。
原文链接: //www.v2ci.com/41.html