P粉5069638422023-08-25 13:05:01
你必须将ref
作为参数传递
<script>
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
name: 'Test',
setup () {
let test = ref(null)
onMounted(() => {
doSomething(test.value)
})
return {
test,
doSomething
}
}
})
function doSomething (param) {
console.log(param); // null
}
</script>
// functions.js import { ref } from 'vue' export let test = ref(null)
// vue-file <script> import { defineComponent, ref, onMounted } from 'vue' import { test } from '../utils/functions.js' export default defineComponent({ name: 'Test', setup () { onMounted(() => { doSomething(test) }) return { test, doSomething } } }) function doSomething (param) { console.log(test.value); // <-- instant access console.log(param.value); // <-- import via parameter } </script>