[Vue warn]: Invalid prop: type check failed for prop "X". Expected , got String
Asked Answered
M

2

8

Given this Vue 2 component:

  Vue.component('read-more', {
    props: {
      'text': String,
      'clamp': {
        type: String,
        default: 'Read More'
      },
      'less': {
        type: String,
        default: 'Read Less'
      },
      'length': {
        type: Number,
        default: 100
      }
    },
    template: `
    <p>
      <span v-if="!show">{{truncate(text)}} <a href="javascript:;" v-if="text.length >= length" @click="toggle()">{{clamp}}</a></span>
      <span v-if="show">{{text}} <a href="javascript:;" @click="toggle()" v-if="text.length >= length">{{less}}</a></span>
    </p>
    `,
    methods: {
      truncate(string) {
        if (string) {
          return string.toString().substring(0, this.length);
        }

        return '';
      },
      toggle() {
        this.show = !this.show;
      },
    },
    data() {
      return {
        show: false,
        counter: this.length,
      };
    },
  });

Usage (HAML):

%read-more{v: {bind: '{text: some_object.property}' }}

Everything works fine but I get Vue warnings for all the declared props:

[Vue warn]: Invalid prop: type check failed for prop "text". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "clamp". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "less". Expected , got String.

[Vue warn]: Invalid prop: type check failed for prop "length". Expected , got Number.

What am I doing wrong?

EDIT

I've created a fiddle which works just fine: http://jsfiddle.net/oLt9wkxe/8/

In my app though, this component is nested within a few other components. It works perfectly fine but shows these warnings which are annoying!

Mews answered 10/7, 2017 at 23:42 Comment(3)
Hi ! Did you figure this out ? edit: in my template I forgot to bind on the property: have to write :prop="true" instead of just prop="true".Pout
@Pout I was already using Vue bindings but still got those errors. In the end, I just ended up removing type checks from my component.Mews
All the info above leads me to believe there's a compilation error somewhere (HAML issue? outdated VueJS?). More info will be needed if the example jsfiddle works without warnings but your app shows warnings. I would recommend taking the jsfiddle (simple) example and trying it out in your development environment to see if the warnings show up.Duet
D
1

This attribute will send a string

myBoolValue=false

This attribute will send a boolean

:myBoolValue="false"

This is forcing the type and raise exception

props: { myBoolValue: Boolean }

This won't complain but you will get a string instead of the boolean value

props: [ "myBoolValue" ]

All my code as recomanded example:

const MyTable = Vue.component("my-table", {
props: {
  myBoolValue: Boolean // Force type
  myStringValue: String,
  myObject: Object
},

<my-table
  ref="otbExpertTable_user"
  :myBoolValue="false"
  v-bind:is-mobile="isMobile"
  v-on:hover-item="hoverItem"
  :myObject=metaUsers
/>
Downpipe answered 22/10, 2020 at 2:22 Comment(0)
G
0

You see this warning only on your local version because of the linter which is installed when you create your app via vue-cli.

'{text: some_object.property}' is obviously an object - there is a key and a value.

I don't know HAML specification but I strongly suspect that you should just use bind: 'some_object.property'.

Grandsire answered 19/3, 2018 at 20:17 Comment(3)
I didn't use vue-cli and bind: '{text: some_object.property}' is valid syntax supported by Vue so I would expect it to work (like it does everywhere else)Mews
@Mews Yes it's a valid object syntax but in your code there is: 'text': String. Well then my cli-guess was wrong. It have to be the development mode while on the fiddle probably is the production version used.Grandsire
That's a good guess but the dev version acted the same way. I gave up :/Mews

© 2022 - 2024 — McMap. All rights reserved.