重学vue之Non-Props属性

说明

就是说在父组件中给子组件传递值,但是子组件不在prop中接受

案例

  • 父组件中
<template>
    <div>
        <child oldMsg="hello" oldMsg1="hello1" />
      </div>
</template>
  • 子组件中只有一个div时候
<template>
      <!-- 自动获取全部属性 -->
      <div>child</div>
</template>

开启inheritAttrs: false,会禁止继承

  • 子组件中多个div时候
<template>
      <!-- 获取全部属性 -->
      <div v-bind="$attrs">child</div>
      <!-- 获取指定属性hello并命名为newMsg -->
      <div :newMsg="$attrs.oldMsg">child</div>
</template>

猜你喜欢

转载自blog.csdn.net/qq_45549336/article/details/110969224