-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"ReferenceError: <var name> is not defined" in production build while mixing "<script>" and "<script setup>" #6677
Comments
Hi. We recently saw a similar issue and our stance is this: Your mixing of script and script setup is invalid. You are using it as a mere shortcut to get around registering components, while using Options API the define the actual component itself. That's not what script setup is designed to support. Here's the explanation of what's going on: By defining a script setup block, that block defines the component. the additional script block is merely there to allow for a few options like The reason your code breaks in production is that when compiling a script setup component for production, the render function compiled from the template is being inlined into the generated
It is compiled with the assumption that all template variables are within the closure of the render function, defined in (script) setup. It does not and - as a matter of complexity - cannot optionally take into account that variables might also have to be accessed through Why does it work in dev? because in dev, in order to allow for better debugability, components are rather compiled like this: export default {
setup() {
const msg = ref()
return {
msg,
}
},
render() {
// render function code here
// can use msg, and CAN use name
},
data: () => ({
name: 'Jack'
})
} So what will happen with this issue?
|
Thanks for the detailed explanation of what happens under the hood, it's heavily interesting! 👌 TBH, I was not having the feeling of doing something "wrong", even after carefully read the documentation. Now I figure out that I have to make a "per component" choice regarding the used API and my preference will go to option API in most cases, without a doubt. Let me explain why. Personal background: Framework comparison: Args exposed in documentation vs personal experience In addition, my experience let me think that most of the args lack of subtlety in their assertions. Let's take them one by one:
Sorry for the very long comment, I hope my point of view and experience sharing could be helpful for Vue dev team and community. I try to be as objective as possible, in the hope it could give some reasons to think about the benefits if Vue could intentionally support both API usage in the same component, and so we could have "best of both worlds" everywhere 😍 |
I can understand your POV, and we left both APIs available precisely because many people prefer the Options API. However I don't really see how this relates to the issue being discussed here.
Yep, as I said we need to be much clearer here. |
I'm explaining all of that to demonstrate why it could be a good feature to support both API usage in the same component:
Anyway, thanks A LOT for deep explanation and clarification about the intention behind. Thanks to you, I have updated my component to stop using |
So.. here we state that mixing both styles is an antipattern. I agree tha using |
It's a good addition... when you need it. From my personal experience, most of the time, it's not needed. So go for options API most of the time, and use composition API for the rest 🤷♂️ |
I've spent about 10 hours trying to debug my code until I finally found this issue and learned that <script setup> and <script> shouldn't be mixed. I emphasize that this needs to be better documented. |
Actually, this should work as intended. The root cause of the problem is that the codegen for This is now fixed via 15e889a |
Vue version
3.2.39
Link to minimal reproduction
https://github.com/M1CK431/demo-computed-issue
Steps to reproduce
yarn serve --mode=production
What is expected?
The underlying form data should be updated without any console error.
What is actually happening?
The underlying form data isn't updated and a console error appears:
ReferenceError: thing is not defined
System Info
System: OS: Linux 5.19 Arch Linux CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz Memory: 9.15 GB / 15.37 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: Node: 18.9.0 - /usr/bin/node Yarn: 1.22.19 - /usr/bin/yarn npm: 8.19.2 - /usr/bin/npm Browsers: Firefox: 104.0.2 npmPackages: vue: ^3.2.33 => 3.2.39
Any additional comments?
The issue occurred only in production build. Everything works as expected in development build.
The key here is the
<script setup>
usage inApp.vue
.I have found two way to workaround this issue:
<script>
to import componentsThe text was updated successfully, but these errors were encountered: