vue3簡(jiǎn)單封裝input組件和統(tǒng)一表單數(shù)據(jù)詳解
前言
vue3 支持用 jsx 實(shí)現(xiàn)組件,擺脫了 vue 文件式的組件,不再需要額外的指令,寫法非常接近 React,減少記憶負(fù)擔(dān)。
本文簡(jiǎn)單的練習(xí),用 vue3 組件封裝 input 組件和統(tǒng)一表單數(shù)據(jù)。
準(zhǔn)備工作
用vue create example
創(chuàng)建項(xiàng)目,參數(shù)大概如下:
用原生 input
原生的 input,主要是 value 和 change,數(shù)據(jù)在 change 的時(shí)候需要同步。
App.tsx如下:
import { ref } from 'vue'; export default { setup() { // username就是數(shù)據(jù) const username = ref('張三'); // 輸入框變化的時(shí)候,同步數(shù)據(jù) const onInput = ; return () => ( <div> <input type="text" value={username.value} onInput={(e: any) => { username.value = e.target.value; }} /> <div>input的值:{username.value}</div> </div> ); }, };
封裝 Input
封裝 input 的好處,直接傳值,減少邏輯,不再需要額外的e.target
,為后面的繼續(xù)封裝做準(zhǔn)備。
// Input.tsx import { defineComponent, ref } from 'vue'; // defineComponent定義組件,有props const Input = defineComponent({ props: { value: { required: true, type: String, }, onChange: { required: true, type: Function, }, }, // 渲染用到props,需要在這里傳參 setup(props) { // 值變化 的時(shí)候 調(diào)用傳過(guò)來(lái)的onChange從而同步父組件的 數(shù)據(jù) const onInput = (e: any) => { props.onChange && props.onChange(e.target.value); }; return () => <input type="text" value={props.value} onInput={onInput} />; }, });
使用Input組件
import { ref } from 'vue'; import Input from './components/Input'; export default { setup() { // 數(shù)據(jù) const username: any = ref('張三'); return () => ( <div> {/* 使用組件,傳value和onChange */} <Input value={username.value} onChange={(value: string) => (username.value = value)} // 直接在這同步數(shù)據(jù) /> <div>input的值:{username.value}</div> </div> ); }, };
封裝表單數(shù)據(jù)
表單數(shù)據(jù),經(jīng)常需要賦值、獲取值,這邊可以用類統(tǒng)一處理,在后面的組件賦值屬性的時(shí)候極其方便。
useForm的精華,在于proxy,訪問(wèn)屬性的時(shí)候,返回field數(shù)據(jù),這在表單組件里可以簡(jiǎn)潔使用。
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { ref, Ref } from "vue"; export class FormData<T> { private data: Ref<any>; constructor(data: T) { this.data = ref(data || null); } // 設(shè)置某個(gè)字段的值 setValue(name: string, val: any): void { const next = { ...this.data.value, [name]: val }; this.data.value = next; } // 獲取某個(gè)字段的值 getValue(name: string): any { return this.data.value[name]; } // 獲取整個(gè)值 getValues() { return this.data.value; } // 設(shè)置整個(gè)值 setValues(values: T) { this.data.value = values; } // 獲取field,字段和字段的修改事件 getField(name: string) { return { value: this.data.value[name], onChange: (v: any) => { this.setValue(name, v); }, }; } } type FormDataProxy<T> = { [P in keyof T]: T[P]; }; export function useForm<T extends Record<string, any>>(data: T) { const form = new FormData(data); const ver = ref(0); const proxy = new Proxy(form, { // 寫proxy的目的是:form.username的時(shí)候,直接返回 form.getField(username) get(target, name) { switch (name) { case "getValues": return form.getValues.bind(form); case "setValues": return form.setValues.bind(form); default: return form.getField(name as string); } }, // 寫form.username = xx 直接返回 form.setValue('username',xx) set(target, name, value) { switch (name) { case "getValues": case "setValues": break; default: form.setValue(name as string, value); } return true; }, }) as any as FormDataProxy<T> & { setValues: (val: T) => void; getValues: () => Ref<T>; }; return { form: proxy, ver }; }
使用表單數(shù)據(jù)
Input組件配合表單,使用效果奇佳。
import Input from './components/Input'; import { useForm } from './hooks/useForm'; // 使用組件 export default { setup() { // 數(shù)據(jù) const { form, ver } = useForm({ username: '張三', age: 33 }); console.log(123, form, ver); return () => ( <div> {/* 這里的form.username,實(shí)際是proxy返回的{value:xxx,onChange:fn} */} {/* 多表單組件的時(shí)候 這樣就非常方便了 */} <Input {...form.username} /> <Input {...form.age} /> <button onClick={() => { console.log(form.getValues()); }} > 提交 </button> </div> ); }, };
總結(jié)
到此這篇關(guān)于vue3簡(jiǎn)單封裝input組件和統(tǒng)一表單數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue3封裝input組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue學(xué)習(xí)筆記進(jìn)階篇之vue-cli安裝及介紹
這篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之vue-cli安裝及介紹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07解決報(bào)錯(cuò)ValidationError: Progress Plugin Invalid&
這篇文章主要介紹了解決報(bào)錯(cuò)ValidationError: Progress Plugin Invalid Options問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11vue實(shí)現(xiàn)瀑布流組件滑動(dòng)加載更多
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)瀑布流組件滑動(dòng)加載更多,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03vue-cli3 DllPlugin 提取公用庫(kù)的方法
這篇文章主要介紹了vue-cli3 DllPlugin 提取公用庫(kù) ,需要的朋友可以參考下2019-04-04