question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vue2 tsx (class component) hmr exception

See original GitHub issue

Describe the bug

vite@3 + vue2 + class component + tsx

When use lang="tsx" and class component, the vite hmr error.

See below about “Steps to reproduce” for details.

Reproduction

Steps to reproduce

dependencies

{
  "@vitejs/plugin-vue2": "^2.0.1",
  "@vitejs/plugin-vue2-jsx": "^1.0.3",
  "vite": "^3.2.3",
  "vue": "^2.7.14",
  "vue-class-component": "^7.2.6",
  "vue-property-decorator": "^9.1.2"
}

set lang="tsx"

<template>
  <el-form
    ref="form"
    :mode="value"
    size="mini"
    @submit.native.prevent
  >
    <el-form-item label="Test">
      <!-- e.g.: changed "v-model" to test1,  -->
      <el-input type="input" v-model="test" clearable />
    </el-form-item>
  </el-form>
</template>

<script lang="tsx">
import Vue from 'vue';
import { Component, Prop, Watch } from 'vue-property-decorator';
import type { TestProps } from '../types';

@Component({
  name: 'Test',
})
export default class Test extends Vue {
  @Prop()
  readonly value!: TestProps;

  // e.g.: changed "get test" to test1 
  get test() {
    return this.value.test;
  }
}
</script>

an error occurred when changed the get property, e.g.:

  1. changed v-model and get test to test1

  2. vite hmr

  3. we can find two requests in the network

    • .../form/index.vue?t=1668996911179

      It‘s right, the content is as follows

      image

    • .../form/index.vue?t=1668996911179&vue&type=script&lang.tsx

      Here is error, the content is still test

      image

Everything works fine when not using tsx (set lang="ts"), because only one hmr request (.../form/index.vue?t=1668996911179).

And, if I continue to add the set test1() {}, even if I add a new line, sometimes, the hmr request content is fine.

System Info

Chrome 107.0.5304.62 (arm64)

Used Package Manager

yarn

Logs

The error logs:

[Vue warn]: Property or method "test1" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

Validations

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sun0daycommented, Nov 25, 2022

@sun0day thanks for the fix! Has this issue been created also in the vite-plugin-vue2 repo? Are you planing to send a PR there too?

I’d love to, I will give a try

2reactions
sun0daycommented, Nov 24, 2022

The reproduction is using @vitejs/plugin-vue2 and @vitejs/plugin-vue2-jsx. These plugins are not maintained in this repo. It’s https://github.com/vitejs/vite-plugin-vue2 and https://github.com/vitejs/vite-plugin-vue2-jsx.

@sun0day Do you mean this happens with plugin-vue + plugin-vue-jsx as well? If so I think it’s better to create a PR directly because it’s easier to discuss.

The handleHotUpdate between plugin-vue2 and plugin-vue looks the same.

https://github.com/vitejs/vite-plugin-vue2/blob/eaa7886b2a0359af2589d4d31412c5136f60c3b9/src/handleHotUpdate.ts#L54-L68

Plus, I can ensure this happens in plugin-vue too.

See https://stackblitz.com/edit/vitejs-vite-r6uw9g?file=vite.config.js,index.html,src%2Fmain.ts,src%2Fhelloworld.vue,src%2FApp.vue,src%2Fassets%2Fbase.css,src%2Fassets%2Fmain.css,src%2Fassets%2Flogo.svg,package.json

I will give a try to offer a PR, let’s discuss there. @sapphi-red

Read more comments on GitHub >

github_iconTop Results From Across the Web

HMR does not work with class-components loaded in the router
In the 'App.vue' I created a class component in which HMR does work, try changing the text and you see it updated on...
Read more >
Vue Typescript @Component throwing compile error. with vue ...
I am using vue-class-component and vue-property-decorator. I have followed almost every setup I could find and still cannot resolve this.
Read more >
@vue/compat | Yarn - Package Manager
Dependencies that rely on Vue 2 internal APIs or undocumented behavior. The most common case is usage of private properties on VNodes ....
Read more >
JS-0239 · Consider using `let` or `const` instead of `var`
1064 // exception: calling readonly() on a reactive object 1065 if ... 1786 console.warn(`[HMR] Something went wrong during Vue component hot-reload.
Read more >
Encore, Symfony & API Platform > The Delightful World of Vue
In this tutorial, we'll be using Vue version 2. But even as I'm saying this, Vue version 3 is nearing release and might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found