vue-property-decorator icon indicating copy to clipboard operation
vue-property-decorator copied to clipboard

Boolean variable is not set as true, when passed without `v-bind`

Open cLupus opened this issue 5 years ago • 6 comments

Hi, I am unable to have a boolean property set to true, by specifying it in the parent component.

An example of the parent

<div>
    <child-component
        be-awesome
    />
</div>
import { Component, Vue } from 'vue-property-decorator'
import ChildComponent from './child'

@Component({ components: { ChildComponent } })
export default class ParentComponent extends Vue {
/* Here be some methods, getters, and such */
}

An example of the child

<div>
    <span v-if="beAwesome"> Cool </span>
<div/>
import { Component, Prop, Vue } from 'vue-property-decorator'
@Component
export default class ChildComponent extends Vue {
    @Prop({ default: false, type: Boolean })
    readonly beAwesome: boolean
}

However, the parent component does not render as expected.

cLupus avatar Mar 28 '19 07:03 cLupus

for me it works, if I do the following:

import { Component, Prop, Vue } from 'vue-property-decorator'
@Component
export default class ChildComponent extends Vue {
    @Prop({ type: Boolean })
    private beAwesome: Boolean
}

Edit: oh, tslint hates private beAwesome: Boolean :/

schmulschubiak avatar Apr 07 '19 12:04 schmulschubiak

In ny case, I am using reflect-metadata for dropping the extraneous JavaScrip type deceleration.

Also, in my case, tslint is not an issue, as I’m using an ESLint plugin for linting .ts files.

cLupus avatar Apr 11 '19 13:04 cLupus

I can validate that this is not working as expected.

  @Prop() title!: string;
  @Prop(Boolean) secondary!: boolean;

I need to speciefy Boolean in the @Prop(Boolean), or use @Prop({ type: Boolean}).

But when I use a string or some other property this does not seem to be a issue. It would be ideal not need to specify the type.

khrome83 avatar May 11 '19 02:05 khrome83

Thanks, this was driving me crazy. Adding { type: boolean } works as a fix but doesn't seem like it should be necessary.

teledemic avatar Aug 24 '20 20:08 teledemic

Install reflect-metadata should do the trick.

This information is on documentation : https://github.com/kaorun343/vue-property-decorator#if-youd-like-to-set-type-property-of-each-prop-value-from-its-type-definition-you-can-use-reflect-metadata

CostaIsNotAvailable avatar Dec 13 '21 09:12 CostaIsNotAvailable

Thanks, this was driving me crazy. Adding { type: boolean } works as a fix but doesn't seem like it should be necessary.

I, too, don't want to set this { type: boolean } every time.

WeijianXu avatar Jan 25 '22 09:01 WeijianXu