自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于 Vue3 和 TypeScript 項目大量實踐后的思考

開發(fā) 前端
Vue3出來已經(jīng)有一段時間了,在團隊中,也進行了大量的業(yè)務實踐,也有了一些自己的思考。

 [[435877]]

概述

Vue3出來已經(jīng)有一段時間了,在團隊中,也進行了大量的業(yè)務實踐,也有了一些自己的思考。

總的來說,Vue3無論是在底層的原理上,還是在業(yè)務的實際開發(fā)中,都有了長足的進步。

使用 proxy 代替之前的 Object.defineProperty 的API,性能更加優(yōu)異,也解決了之前vue在處理對象、數(shù)組上的缺陷;在diff算法上,使用了靜態(tài)標記的方式,大大提升了Vue的執(zhí)行效率。

在使用的層面,我們從options Api,變成了composition Api,慢慢的在實際的業(yè)務中,我們拋棄了原本的data、methods、computed那種隔離式的寫法。compositon Api,它更加聚焦,它講究的是相關業(yè)務的聚合性。同時,在composition Api中,為了防止過于重的業(yè)務邏輯,它提供了一種關注點分離的方式,大大的提升了我們代碼的可讀性。

完全良好的支持了TypeScript,類型校驗也成為了以后Vue3進行大型項目開發(fā)的質(zhì)量保障,同時這也是面向了趨勢 -- 前端的未來就是TypeScript!

一、compositon Api

compositon Api的本質(zhì),體現(xiàn)在代碼里面,也就是一個setup函數(shù),在這個setup函數(shù)中,返回的數(shù)據(jù),會用到該組件的模板中。return的這個對象,一定程度上,代表了之前vue2中的data屬性。

  1. import { defineComponent, ref } from 'vue'
  2. export default defineComponent({ 
  3.     name: 'Gift'
  4.     setup() { 
  5.         const counter = ref(0); 
  6.         return { 
  7.             counter 
  8.         } 
  9.     } 
  10. }) 

這時候,對于大多數(shù)初學者來說,可能存在的疑惑就是,那么我能不能定義options Api的寫法,比如data、computed、watch、methods等等。

這里我需要明確的是,Vue3是完全兼容Vue2的這種options Api的寫法,但是從理念上來說,更加推薦setup的方式,來寫我們的組件。原因如下:Vue3的存在,本身是為了解決Vue2的問題的,Vue2的問題就是在于,聚合性不足,會導致代碼越來越臃腫!setup的方式,能夠讓data、方法邏輯、依賴關系等聚合在一塊,更方便維護。

也就是說,以后我們盡量不要寫單獨的data、computed、watch、methods等等,不是Vue3不支持,而是和Vue3的理念違背。

components屬性,也就是一個組件的子組件,這個配置在Vue2和3的差異不大,Vue2怎么用,Vue3依然那么用。

1、ref 和 reactive的區(qū)別?

在功能方面,ref 和 reactive,都是可以實現(xiàn)響應式數(shù)據(jù)!

在語法層面,兩個有差異。ref定義的響應式數(shù)據(jù)需要用[data].value的方式進行更改數(shù)據(jù);reactive定義的數(shù)據(jù)需要[data].[prpoerty]的方式更改數(shù)據(jù)。

  1. const actTitle: Ref<string> = ref('活動名稱'); 
  2.  
  3. const actData = reactive({ 
  4.     list: [], 
  5.     total: 0
  6.     curentPage: 1
  7.     pageSize: 10 
  8. }); 
  9.  
  10. actTitle.value = '活動名稱2'
  11.  
  12. actData.total = 100

但是在應用的層面,還是有差異的,通常來說:單個的普通類型的數(shù)據(jù),我們使用ref來定義響應式。表單場景中,描述一個表單的key:value這種對象的場景,使用reactive;在一些場景下,某一個模塊的一組數(shù)據(jù),通常也使用reactive的方式,定義數(shù)據(jù)。

那么,對象是不是非要使用reactive來定義呢?其實不是的,都可以,根據(jù)自己的業(yè)務場景,具體問題具體分析!ref他強調(diào)的是一個數(shù)據(jù)的value的更改,reactive強調(diào)的是定義的對象的某一個屬性的更改。

2、周期函數(shù)

周期函數(shù),在Vue3中,是被單獨使用的,使用方式如下:

  1. import { defineComponent, ref, onMounted } from 'vue'
  2. export default defineComponent({ 
  3.     name: 'Gift'
  4.     setup() { 
  5.         const counter = ref(0); 
  6.         onMounted(() => { 
  7.             // 處理業(yè)務,一般進行數(shù)據(jù)請求 
  8.         }) 
  9.         return { 
  10.             counter 
  11.         } 
  12.     } 
  13. }) 

3、store使用

在Vue2中,其實可以直接通過this.$store進行獲取,但是在Vue3中,其實沒有this這個概念,使用方式如下:

  1. import { useStore } from "vuex"
  2. import { defineComponent, ref, computed } from 'vue'
  3. export default defineComponent({ 
  4.     name: 'Gift'
  5.     setup() { 
  6.         const counter = ref(0); 
  7.         const store = useStore(); 
  8.         const storeData = computed(() => store); // 配合computed,獲取store的值。 
  9.         return { 
  10.             counter, 
  11.             storeData 
  12.         } 
  13.     } 
  14. }) 

4、router的使用

在Vue2中,是通過this.$router的方式,進行路由的函數(shù)式編程,但是Vue3中,是這么使用的:

  1. import { useStore } from "vuex"
  2. import { useRouter } from "vue-router"
  3. import { defineComponent, ref, computed } from 'vue'
  4. export default defineComponent({ 
  5.     name: 'Gift'
  6.     setup() { 
  7.         const counter = ref(0); 
  8.         const router = useRouter(); 
  9.         const onClick = () => { 
  10.             router.push({ name: "AddGift" }); 
  11.         } 
  12.         return { 
  13.             counter, 
  14.             onClick 
  15.         } 
  16.     } 
  17. }) 

二、關注點分離

關注點分離,應該分兩層意思:第一層意思就是,Vue3的setup,本身就把相關的數(shù)據(jù),處理邏輯放到一起,這就是一種關注點的聚合,更方便我們看業(yè)務代碼。

第二層意思,就是當setup變的更大的時候,我們可以在setup內(nèi)部,提取相關的一塊業(yè)務,做到第二層的關注點分離。

  1. import { useStore } from "vuex"
  2. import { useRouter } from "vue-router"
  3. import { defineComponent, ref, computed } from 'vue'
  4. import useMerchantList from './merchant.js'
  5. export default defineComponent({ 
  6.     name: 'Gift'
  7.     setup() { 
  8.         const counter = ref(0); 
  9.         const router = useRouter(); 
  10.         const onClick = () => { 
  11.             router.push({ name: "AddGift" }); 
  12.         } 
  13.         // 在該示例中,我們把獲取商家列表的相關業(yè)務分離出去。也就是下面的merchant.ts 
  14.         const {merchantList} = useMerchantList(); 
  15.         return { 
  16.             counter, 
  17.             onClick, 
  18.             merchantList 
  19.         } 
  20.     } 
  21. }) 

merchant.ts

  1. import { getMerchantlist } from "@/api/rights/gift"
  2. import { ref, onMounted } from "vue"
  3.  
  4. export default function useMerchantList(): Record<string, any> { 
  5.   const merchantList = ref([]); 
  6.   const fetchMerchantList = async () => { 
  7.     let res = await getMerchantlist({}); 
  8.     merchantList.value = res?.data?.child; 
  9.   }; 
  10.  
  11.   onMounted(fetchMerchantList); 
  12.  
  13.   return { 
  14.     merchantList 
  15.   }; 

三、TypeScript支持

這一部分內(nèi)容,準確的來說,是TS的內(nèi)容,不過它與Vue3項目開發(fā),息息相關,所以真的想用Vue3,我們還是得了解TS的使用。

不過這一部分,我不會介紹TS的基礎語法,主要是在業(yè)務場景中,如何組織TS。

使用TS進行業(yè)務開發(fā),一個核心的思維是,先關注數(shù)據(jù)結(jié)構(gòu),再根據(jù)數(shù)據(jù)結(jié)構(gòu)進行頁面開發(fā)。以前的前端開發(fā)模式是,先寫頁面,后關注數(shù)據(jù)。

比如要寫一個禮品列表的頁面,我們可能要定義這么一些interface??偠灾覀冃枰P注的是:頁面數(shù)據(jù)的interface、接口返回的數(shù)據(jù)類型、接口的入?yún)㈩愋偷鹊取?/p>

  1. // 禮品創(chuàng)建、編輯、列表中的每一項,都會是這個數(shù)據(jù)類型。 
  2. interface IGiftItem { 
  3.   id: string | number; 
  4.   name: string; 
  5.   desc: string; 
  6.   [key: string]: any; 
  7.  
  8. // 全局相應的類型定義 
  9. // 而且一般來說,我們不確認,接口返回的類型到底是什么(可能是null、可能是對象、也可能是數(shù)組),所以使用范型來定義interface 
  10. interface IRes<T> { 
  11.     code: number; 
  12.     msg: string; 
  13.     data: T 
  14. // 接口返回數(shù)據(jù)類型定義 
  15.  
  16. interface IGiftInfo { 
  17.     list: Array<IGiftItem>; 
  18.     pageNum: number; 
  19.     pageSize: number; 
  20.     total: number; 

在一個常見的接口請求中,我們一般使用TS這么定義一個數(shù)據(jù)請求,數(shù)據(jù)請求的req類型,數(shù)據(jù)請求的res類型。

  1. export const getGiftlist = ( 
  2.   params: Record<string, any> 
  3. ): Promise<IRes<IGiftInfo>> => { 
  4.   return Http.get("/apis/gift/list", params); 
  5. }; 

 

 

責任編輯:張燕妮 來源: 三分鐘學前端
相關推薦

2021-05-26 10:40:28

Vue3TypeScript前端

2020-12-01 08:34:31

Vue3組件實踐

2020-09-17 07:08:04

TypescriptVue3前端

2023-04-02 10:06:24

組件vue3sign2.

2024-04-08 07:28:27

PiniaVue3狀態(tài)管理庫

2021-12-01 08:11:44

Vue3 插件Vue應用

2022-04-07 08:06:32

viteVue3項目

2024-03-27 08:41:09

Vue3Web應用emoji表情選擇器

2021-11-30 08:19:43

Vue3 插件Vue應用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-10-21 06:52:17

Vue3組件 API

2024-04-16 07:46:15

Vue3STOMP協(xié)議WebSocket

2021-12-02 05:50:35

Vue3 插件Vue應用

2020-09-19 21:15:26

Composition

2020-10-25 18:43:20

VueTypeScript前端

2021-05-27 10:36:34

ProvideInjectVue3

2022-08-17 11:36:18

Vue3插件

2023-08-24 08:37:50

VueCSS

2022-12-16 17:09:57

2021-12-08 09:09:33

Vue 3 Computed Vue2
點贊
收藏

51CTO技術棧公眾號