Invalid prop type from router params, expected Number got String

abu abu

I am trying to collect id value from URL.

Previously I take help in this post.

In my index.js of router file I have below code:

{   path: '/word/:id',
    name: 'word',   
    component: Word,
    props: true,
}, 

In my component I have below code:

<script>
    export default {
        props: {
             id: Number,
        },
        created() {
             this.routeChanged();
        },
        watch: {
            'id': 'routeChanged',
        },
        methods: {
            routeChanged () {
                console.log(this.id);
            },
        },
    };
</script>

I am getting below error:

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

ghybs

That is the use case for Passing Props to Route Components with Function mode:

You can create a function that returns props. This allows you to cast parameters into other types, combine static values with route-based values, etc.

In your case, instead of specifying the route option with props: true, you would pass a function to the props option:

routes = [{
  path: '/word/:id',
  component: Word,
  props: castRouteParams
}];

function castRouteParams(route) {
  return {
    id: Number(route.params.id),
  };
}

Live example: https://codesandbox.io/s/8kyyw9w26l (click on the "Go to Test/3" link)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

parse.com invalid type for key, expected string, but got array

invalid type for key Users, expected *_User, but got string

React.createElement: type is invalid -- expected a string, but got: object

Redux-form, Invalid prop 'value' of type 'number' supplied to 'TextInput', expected 'string'

React router error (Failed prop type: Invalid prop `children` supplied to `Switch`, expected a ReactNode.)

Failed prop type: Invalid prop `match.params` of type `object` supplied to `Page`, expected `string`

Failed prop type: Invalid prop `children` of type `string` supplied to `DetailField`, expected `object`

Invalid prop: type check failed for prop "items". Expected Array, got String

Invalid prop: type check failed for prop "data". Expected Array, got Object

Element type is invalid: expected a string or a class/function but got: undefined

invalid prop value of type number supplied to textinput expected string react native

Vuejs error, Invalid prop: type check failed for prop. Expected Date, got Number with value

GraphQL: Warning: Failed prop type: Invalid prop `query` of type `object` supplied to `StaticQuery`, expected `string`

Warning: Failed prop type: Invalid prop `value` of type `number` supplied to `TextInput`, expected `string`

VueJs - Invalid prop: type check failed for prop "page". Expected DocumentPageDto, got Object

Invalid prop: type check failed for prop "value". Expected Boolean, got String with value "0"

Vue.js - Invalid prop: type check failed for prop "src". Expected String, Object, got Promise

invalid type for key Age, expected string, but got number

Element type is invalid: expected a string/class/function got: object

React createElement error "type is invalid -- expected a string got object"

vue.js vuetify test-utils warning : Vue warn]: Invalid prop: type check failed for prop "src". Expected String, got Object

Why do I get this "Invalid prop: expected Array, got String" error?

How to pass ref as prop: [Vue warn]: Invalid prop: type check failed for prop "containerRef". Expected Object, got HTMLDivElement?

VueJs: Invalid prop:Expected Boolean, got Undefined

Invalid prop: type check failed for prop "cellClass". Expected String with value "[object Object]", got Object

[Vue warn]: Invalid prop: type check failed for prop "productCartData". Expected Object, got String with value "[object Object]"

vue warning: Invalid prop: type check failed for prop "modalState". Expected Boolean, got Function

Warning: Failed prop type: Invalid prop `defaultValue` of type `string` supplied to `AutoCompleteSearch`, expected `function`

[Vue warn]: Invalid prop: type check failed for prop "page". Expected Number with value 0, got String with value ""