diff --git a/src/App.vue b/src/App.vue index ce610c83336d713979cb6a20865004e944521099..82a2506458633e89d369d66c208bc2f006616663 100644 --- a/src/App.vue +++ b/src/App.vue @@ -18,3 +18,26 @@ let name = '小明' +import { reactive, toRefs } from 'vue' + +function myFunction() { + const state = reactive({ + foo: 'bar' + }) + + function updateFoo(newFoo) { + state.foo = newFoo + } + + return { + ...toRefs(state), + updateFoo + } +} + +// 在函数外部使用 +const { foo, updateFoo } = myFunction() +console.log(foo.value) // 输出:'bar' + +updateFoo('new value') +console.log(foo.value) // 输出:'new value'