1 回答

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
您應(yīng)該從 vue 導(dǎo)入provide并在 setup 函數(shù)中使用:
import {ref,provide} from "vue"
export default {
name: "MyComponent",
setup() {
const myVariableThatIWantToProvide = ref('test');
provide ('myVariableThatIWantToProvide', myVariableThatIWantToProvide )
return {
myVariableThatIWantToProvide
};
}
};
</script>
在孫子組件中:
import {inject} from "vue"
export default {
name: "somechild",
setup() {
const myVariableThatIWantToProvide =inject ('myVariableThatIWantToProvide')
return {
myVariableThatIWantToProvide
};
}
};
</script>
添加回答
舉報(bào)