有人知道如何提供设置函数中的变量吗?export default { name: "MyComponent", provide: { myVariableThatIWantToProvide // This is not working }, setup() { const myVariableThatIWantToProvide = ref('test'); return { myVariableThatIWantToProvide }; }};</script>
1 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
您应该从 vue 导入provide并在 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>
添加回答
举报
0/150
提交
取消