Vue, I pass a number, and I receive string?.

write a component by yourself, which is passed to the subcomponent as number, but the result subcomponent receives stringtype

.

parent component:

<cmsProgress num=40></cmsProgress>

Sub-component:

<el-progress :text-inside="true" :stroke-width="20" :percentage="num" color="red"></el-progress>

export default {
    props:["num"],
    data () {
        return {
            
        }
    },
    mounted:function(){
        console.log(typeof(this.num));  //string
    }
}

what is the reason for this?


this is why the HTML tag attribute value itself is the string . For more detailed interpretation, please refer to Portal

.

clipboard.png

so the code should be changed to the following:

<cmsProgress :num="40"></cmsProgress>

to get Number , follow @ Wenbo mentioned : num= "40" so

it's not really the vue problem, but any attributes in the html element itself will be parsed to string (even if you're not using vue ).

Menu