Skip to content

Props defined using Vue.with(Props) are not populated by element attributes #501

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

Closed
bobmeyers5 opened this issue Dec 26, 2020 · 2 comments
Closed

Comments

@bobmeyers5
Copy link

When I use the @Options({ props: ... }) syntax currently generated by vue-cli, the component props are correctly populated by element attributes. When I use the extends Vue.with(Props) syntax, the component props are not populated.

Repro:
I used [email protected] with Vue3, babel, typescript, and eslint support to generate a new project.

Dependencies added include:

"vue": "^3.0.0",
"vue-class-component": "^8.0.0-0"

HelloWorld.vue (minus some fluff) looks like this:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';

@Options({
  props: {
    msg: String
  }
})
export default class HelloWorld extends Vue {
  msg!: string
}
</script>

App.vue uses the component like this:

  <HelloWorld msg="My favorite prop value"/>

This works just fine. The attribute value is correctly displayed in the <h1> element inside the component. I'm not sure why the generated class defines msg as both a prop and data, though.

If I change the component to use the new prop syntax (and comment out the data property), like this:

class Props {
  msg!: string
}

export default class HelloWorld extends Vue.with(Props) {
  //msg!: string
}

The app compiles and loads just fine, but the prop is no longer populated by the element attribute.

This test case suggests this should work, so I'm not sure what's broken:

it('props class', () => {

Changing the dependency version to 8.0.0-rc.1 did not affect the behavior.

@ygj6
Copy link
Member

ygj6 commented Dec 28, 2020

Hi Bob, you need to add "useDefineForClassFields": true for TypeScript compiler option, or define prop like this:

import { Vue, prop } from 'vue-class-component'
class Props {
  msg = prop({
    type: String,
    required: true
  })
}

@ktsn Shall we add "useDefineForClassFields":true to vue-cli's template project ?

@ktsn
Copy link
Member

ktsn commented Jan 24, 2021

As @ygj6 said.

I've made a PR in vue-cli repo.
vuejs/vue-cli#6235

@ktsn ktsn closed this as completed Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants