vue2和vue3中provide/inject的基本用法示例
前言
昨天看一個(gè)項(xiàng)目代碼看到了provide,但是學(xué)習(xí)的時(shí)候也沒看到,看了官網(wǎng)才知道vue還有這個(gè)API。多數(shù)情況下,provide會(huì)和inject一起使用,又叫“依賴注入”。
“依賴注入”主要是解決父子組件傳值“props逐級(jí)傳遞”問題。所以,provide/inject的作用就是組件間的傳值。
vue2基本用法:
1.provide
provide
是一個(gè)對(duì)象或是返回一個(gè)對(duì)象的函數(shù)。
寫在祖先組件中,用于提供給子組件可以注入的值。組件的關(guān)系為a-b-c-d
在a組件中將參數(shù)num進(jìn)行傳遞
export default { components: { BCom }, data() { return { num: 2, }; }, provide() { return { num: this.num, }; }, };
2.inject
inject為:一個(gè)數(shù)組,數(shù)組元素為注入的變量
一個(gè)對(duì)象,key為注入的變量,value為一個(gè)包含form和default的對(duì)象
num: { from: 'num', default: '20' }
在d組件中接收注入的變量
寫法一:
export default { inject: ["num"], };
寫法二:
export default { inject: { num: { form: "num", default: 20, }, }, };
可以看到d中顯示的為inject注入的num變量。如果在a中不進(jìn)行provide,則會(huì)顯示默認(rèn)值。
num 不是響應(yīng)式的
點(diǎn)擊+100按鈕,a組件顯示的值改變,d組件顯示的值沒有改變。
如何成為響應(yīng)式?
1.方法一:函數(shù)方法
a組件:
<template> <div style="width: 600px; height: 600px; background-color: darkgreen"> 我是組件a <h4>{{ num }}</h4> <button @click="add">+100</button> <BCom></BCom> </div> </template> <script> import BCom from "./b-com.vue"; export default { components: { BCom }, data() { return { num: 2, }; }, provide() { return { num: () => this.num, }; }, methods: { add() { this.num = this.num + 100; }, }, }; </script> <style> </style>
b組件
<template> <div style=" width: 300px; height: 300px; background-color: bisque; " > 我是組件d <h4>{{ this.num() }}</h4> </div> </template> <script> export default { inject: { num: { form: "num", default: () => {}, }, }, }; </script> <style> </style>
2.方法二:傳遞this
a組件
<template> <div style="width: 600px; height: 600px; background-color: darkgreen"> 我是組件a <h4>{{ num }}</h4> <button @click="add">+100</button> <BCom></BCom> </div> </template> <script> import BCom from "./b-com.vue"; export default { components: { BCom }, data() { return { num: 2, }; }, provide() { return { AThis: this, }; }, methods: { add() { this.num = this.num + 100; }, }, }; </script> <style> </style>
d組件
<template> <div style=" width: 300px; height: 300px; background-color: bisque; " > 我是組件d <h4>{{ this.AThis.num }}</h4> </div> </template> <script> export default { inject: { AThis: { form: "AThis", default() { return {}; }, }, }, }; </script> <style> </style>
vue3的基本用法:
provide()
接受兩個(gè)參數(shù):第一個(gè)參數(shù)是要注入的 key,可以是一個(gè)字符串或者一個(gè) symbol,第二個(gè)參數(shù)是要注入的值。
<script setup> import { ref, provide } from 'vue' // 提供靜態(tài)值 provide('num') // 提供響應(yīng)式的值 const count = ref(0) provide('count', count) </script>
inject:
第一個(gè)參數(shù)是注入的 key。
Vue 會(huì)遍歷父組件鏈,通過匹配 key 來確定所提供的值。如果父組件鏈上多個(gè)組件對(duì)同一個(gè) key 提供了值,那么離得更近的組件將會(huì)“覆蓋”鏈上更遠(yuǎn)的組件所提供的值。如果沒有能通過 key 匹配到值,
inject()
將返回undefined
,除非提供了一個(gè)默認(rèn)值。第二個(gè)參數(shù)是可選的,即在沒有匹配到 key 時(shí)使用的默認(rèn)值。它也可以是一個(gè)工廠函數(shù),用來返回某些創(chuàng)建起來比較復(fù)雜的值。如果默認(rèn)值本身就是一個(gè)函數(shù),那么你必須將
false
作為第三個(gè)參數(shù)傳入,表明這個(gè)函數(shù)就是默認(rèn)值,而不是一個(gè)工廠函數(shù)。
<script setup> import { inject } from 'vue' // 注入值的默認(rèn)方式 const num= inject('num') // 注入響應(yīng)式的值 const count = inject('count') // 注入一個(gè)值,若為空則使用提供的默認(rèn)值 const bar = inject('foo', 'default value') // 注入一個(gè)值,若為空則使用提供的工廠函數(shù) const baz = inject('foo', () => new Map()) // 注入時(shí)為了表明提供的默認(rèn)值是個(gè)函數(shù),需要傳入第三個(gè)參數(shù) const fn = inject('function', () => {}, false) </script>
注:在d組件中,如果data中存在變量num,inject又注入了變量num,在頁面中會(huì)顯示data中num的值。
總結(jié)
到此這篇關(guān)于vue2和vue3中provide/inject的基本用法的文章就介紹到這了,更多相關(guān)vue中的provide/inject內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js 1.x與2.0中js實(shí)時(shí)監(jiān)聽input值的變化
這篇文章主要介紹了vue.js 1.x與vue.js2.0中js實(shí)時(shí)監(jiān)聽input值的變化的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03在nuxt使用vueX代替storage的實(shí)現(xiàn)方案
這篇文章主要介紹了在nuxt使用vueX代替storage的實(shí)現(xiàn)方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue中使用video.js實(shí)現(xiàn)截圖和視頻錄制與下載
這篇文章主要為大家詳細(xì)介紹了Vue中如何使用video.js實(shí)現(xiàn)截圖和視頻錄制與下載,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-033分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧
最近在學(xué)習(xí)Vue,感覺methods還是有必有總結(jié)一下的,下面這篇文章主要給大家介紹了關(guān)于3分鐘迅速學(xué)會(huì)Vue中methods方法使用技巧的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02結(jié)合康熙選秀講解vue虛擬列表實(shí)現(xiàn)
這篇文章主要為大家介紹了結(jié)合康熙選秀講解vue虛擬列表的原理使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解
這篇文章主要介紹了優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12