注釋:Provide / Inject 可以自定義傳輸?shù)臄?shù)據(jù)是否可響應(yīng)。
注釋:readonly 用來配合 Provide / Inject 保證 Inject 接收的數(shù)據(jù)不能被直接修改。
原文地址 v3.cn.vuejs.org
我們也可以在組合式 API 中使用 provide/inject。兩者都只能在當(dāng)前活動實(shí)例的 setup() 期間調(diào)用。
假設(shè)我們要重寫以下代碼,其中包含一個 MyMap
組件,該組件使用組合式 API 為 MyMarker
組件提供用戶的位置。
<template>
<MyMarker />
</template>
<script>
import MyMarker from './MyMarker.vue'
export default {
components: {
MyMarker
},
provide: {
location: 'North Pole',
geolocation: {
longitude: 90,
latitude: 135
}
}
}
</script>
<script>
export default {
inject: ['location', 'geolocation']
}
</script>
調(diào)用 provide
時來定義每個 property。
provide
函數(shù)允許你通過兩個參數(shù)定義 property:
<String>
類型)使用 MyMap
組件,我們 provide 的值可以按如下方式重構(gòu):
<template>
<MyMarker />
</template>
<script>
import { provide } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
provide('location', 'North Pole')
provide('geolocation', {
longitude: 90,
latitude: 135
})
}
}
</script>
我們就可以調(diào)用它來定義如何將它暴露給我們的組件。
inject
函數(shù)有兩個參數(shù):
使用 MyMarker
組件,可以使用以下代碼對其進(jìn)行重構(gòu):
<script>
import { inject } from 'vue'
export default {
setup() {
const userLocation = inject('location', 'The Universe')
const userGeolocation = inject('geolocation')
return {
userLocation,
userGeolocation
}
}
}
</script>
為了增加 provide 值和 inject 值之間的響應(yīng)性,我們可以在 provide 值時使用 ref 或 reactive。
<template>
<MyMarker />
</template>
<script>
import { provide, reactive, ref } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
const location = ref('North Pole')
const geolocation = reactive({
longitude: 90,
latitude: 135
})
provide('location', location)
provide('geolocation', geolocation)
}
}
</script>
當(dāng)使用響應(yīng)式 provide / inject 值時,建議盡可能,在_提供者_(dá)內(nèi)保持響應(yīng)式 property 的任何更改。
例如,在需要更改用戶位置的情況下,我們最好在 MyMap
組件中執(zhí)行此操作。
<template>
<MyMarker />
</template>
<script>
import { provide, reactive, ref } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
const location = ref('North Pole')
const geolocation = reactive({
longitude: 90,
latitude: 135
})
provide('location', location)
provide('geolocation', geolocation)
return {
location
}
},
methods: {
updateLocation() {
this.location = 'South Pole'
}
}
}
</script>
然而,有時我們需要在注入數(shù)據(jù)的組件內(nèi)部更新 inject 的數(shù)據(jù)。在這種情況下,我們建議 provide 一個方法來負(fù)責(zé)改變響應(yīng)式 property。
<template>
<MyMarker />
</template>
<script>
import { provide, reactive, ref } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
const location = ref('North Pole')
const geolocation = reactive({
longitude: 90,
latitude: 135
})
const updateLocation = () => {
location.value = 'South Pole'
}
provide('location', location)
provide('geolocation', geolocation)
provide('updateLocation', updateLocation)
}
}
</script>
<script>
import { inject } from 'vue'
export default {
setup() {
const userLocation = inject('location', 'The Universe')
const userGeolocation = inject('geolocation')
const updateUserLocation = inject('updateLocation')
return {
userLocation,
userGeolocation,
updateUserLocation
}
}
}
</script>
最后,如果要確保通過 provide
傳遞的數(shù)據(jù)不會被 inject 的組件更改,我們建議對提供者的 property 使用 readonly
。
<template>
<MyMarker />
</template>
<script>
import { provide, reactive, readonly, ref } from 'vue'
import MyMarker from './MyMarker.vue
export default {
components: {
MyMarker
},
setup() {
const location = ref('North Pole')
const geolocation = reactive({
longitude: 90,
latitude: 135
})
const updateLocation = () => {
location.value = 'South Pole'
}
provide('location', readonly(location))
provide('geolocation', readonly(geolocation))
provide('updateLocation', updateLocation)
}
}
</script>