zoukankan      html  css  js  c++  java
  • vue的props和$attrs

    过去我们在vue的父子组件传值的时候,我们先需要的子组件上用props注册一些属性:

    <template>
        <div>
            props:{{name}},{{age}} 或者 {{$props['name']}},{{$props['age']}}
        </div>
    </template>
    
    export default{
        props: ['name','age']
    }

    然后父组件调用的时候当属性来传值

    <child name="rick" :age="18"></child>

    如果我们给child传props没有注册的属性,我们就要用$attrs来取了

    <child name="rick" :age="18" gender="male"></child>

    child:

    <template>
        <div>
            props:{{name}},{{age}} 或者 {{$props['name']}},{{$props['age']}} 
            <br>
            attrs: {{$attrs['gender']}}  在$attrs里面只会有props没有注册的属性
        </div>
    </template>
    
    export default{
        props: ['name','age']
    }

    当然这个$attrs是vue2.4才推出的,为了简化父组件和孙组件的传值:

    父组件 template(假设gender属性没有被props注册):
    <child1 gender="male"></child1>
    child1 template(v-bind=”$attrs”,这是v-bind唯一可以直接跟等号的特殊写法):
    <child2 v-bind=”$attrs”></child2>

    在child2里面,就可以直接用props注册gender,来直接获取来自“祖父组件”的gender值了(当然,不注册也是可以用$attrs来取值的)

  • 相关阅读:
    火车进出栈问题(卡特兰数)
    HDU 4699 Editor (对顶栈)
    HDU 6430 TeaTree (线段树合并)
    Exam 4895 Crowd Control
    Exam 4894 Booming Business
    8377: Playoff
    hdu 6345 Problem J. CSGO
    HDU 6437 Problem L.Videos
    Making the Grade
    poj2279——Mr. Young's Picture Permutations
  • 原文地址:https://www.cnblogs.com/amiezhang/p/8376114.html
Copyright © 2011-2022 走看看