gpt4 book ai didi

javascript - 使用 Vue 3 提供带有设置功能的注入(inject)

转载 作者:行者123 更新时间:2023-12-05 00:31:12 24 4
gpt4 key购买 nike

有人知道如何提供设置函数中的变量吗?

export default {
name: "MyComponent",
provide: {
myVariableThatIWantToProvide // This is not working
},
setup() {
const myVariableThatIWantToProvide = ref('test');

return {
myVariableThatIWantToProvide
};
}
};
</script>

最佳答案

您应该导入 provide来自 vue 并在 setup 函数中使用:

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>

关于javascript - 使用 Vue 3 提供带有设置功能的注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65259629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com