Skip to content
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

new Class() will not construct if the code is in template (eg. @click="() => { new MyClass() }) #217

Closed
7 tasks done
reitowo opened this issue Jul 30, 2023 · 2 comments
Closed
7 tasks done
Labels
bug: upstream Bug in a dependency of this repository

Comments

@reitowo
Copy link

reitowo commented Jul 30, 2023

Describe the bug

I'm writing this code:

<Button label="增加事件" severity="success" @click="() => {
  editingEvent = new GameEvent();
  validateCron(editingEvent.startCron);
  addingEvent = true;
  eventDialogVisible = true;
}" />

After vite build, this code is transformed into

label: "增加事件",
severity: "success",
onClick: x[24] || (x[24] = ()=>{
    k.value = new F(xd)(),
    We(k.value.startCron),
    P.value = !0,
    h.value = !0
}

Where F(xd) is:

function F(e) {
    return kt(e) ? e.value : e
}

function kt(e) {
    return !!(e && e.__v_isRef === !0)
}

Which seems telling if it is a "ref", and the code throw:

TypeError: Class constructor xd cannot be invoked without 'new'
    at X.onClick.x.<computed>.x.<computed> (index-fc7beea5.js:1143:29535)
    at mr (index-fc7beea5.js:1:13064)
    at en (index-fc7beea5.js:1:13143)
    at HTMLButtonElement.n (index-fc7beea5.js:1:57387)

I assume that it is because new F(xd)() is not equal to new xd()?

This seems the same problem:
https://stackoverflow.com/questions/74069642/class-constructor-cannot-be-invoked-without-new-instantiating-own-class-i

Reproduction

The GameEvent has a custom constructor, and it is declared in electron/core/game.ts

export class GameEvent {
    id: string;

    enabled: boolean;
    random: boolean;

    name: string = null;
    content: string = null; 
    startCron: string = "* */1 * * *"; 
    duration: number = null; 

    createsAt: Date = null;

    constructor(init?: Partial<GameEvent>) {
        Object.assign(this, init);
        this.id ??= uuidv4()
    }
}

The Vue page is declared in src/components/Editor.vue

import Button from "primevue/button";
import { GameChoice, GameEvent, GameUserBill } from "../../electron/core/game";

<template>
  <Button label="增加事件" severity="success" @click="() => {
    editingEvent = new GameEvent();
    validateCron(editingEvent.startCron);
    addingEvent = true;
    eventDialogVisible = true;
  }" />
</template>

Steps to reproduce

I use Vue in electron, but this problem should apply to all because it is generated by vite-build

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K
    Memory: 93.70 GB / 127.71 GB
  Binaries:
    Node: 16.18.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1992.0), Chromium (115.0.1901.188)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @vitejs/plugin-vue: ^4.2.1 => 4.2.3
    vite: ^4.3.5 => 4.3.9

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red transferred this issue from vitejs/vite Jul 31, 2023
@sapphi-red
Copy link
Member

This is a bug in Vue: vuejs/core#6483

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2023
@sapphi-red sapphi-red added bug: upstream Bug in a dependency of this repository and removed pending triage labels Jul 31, 2023
@reitowo
Copy link
Author

reitowo commented Jul 31, 2023

Thanks. Hard to find it out.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of this repository
Projects
None yet
Development

No branches or pull requests

2 participants