Script Setup - Class constructor [class] cannot be invoked without 'new'
Vue version
3.2.37
Link to minimal reproduction
Steps to reproduce
- Open link to SFC Playground
- Click the button
- An error
Class constructor [class] cannot be invoked without 'new'
is raised
What is expected?
No error should be visible in <script setup>
as the the equivalent behavior is already working in <script>
What is actually happening?
The code is raising an error Class constructor [class] cannot be invoked without 'new' is raised
System Info
No response
Any additional comments?
See Discussion: https://github.com/vuejs/core/discussions/6480
The compiler produces this code:
(_ctx.$emit('myclick', new _unref(TestClass)())
which is invalid. This should be fixable with an additional set of paranthesis if we need to unwrap directly after new
:
(_ctx.$emit('myclick', new (_unref(TestClass))())
I created a PR for the bug
Guys, any update on this? @yyx990803 It seems multiple same issues happens nowadays, and it is not a rare case, can you reevaluate this issue?
Any update on this issue? Does that mean one should avoid using classes within script/setup? Is there a workaround?
Is it possible to have a follow up on this issue ? The fact that it impacts only the code pushed in production, and not the code in dev mode is especially hurting :'(.
For reference I've been bitten by it both for bindings :var="new Test()"
and triggers @click="test = new Test()"
It is sometimes lighter and more readable to be able to new something directly in the template, but if it is not working in production I would largely prefer to break things in my IDE, rather than having to patch some production code somewhere.