美文网首页VUE
非 Prop 的特性

非 Prop 的特性

作者: lp0124 | 来源:发表于2018-05-16 17:22 被阅读0次

一个非 prop 特性(属性)是指传向一个组件,但是该组件并没有相应 prop 定义的特性。

Vue.component('test-prop', {
  // 在组件props中只定义了param1
  props: ['param1'],
  inheritAttrs: false,  // 详情见 ↓ 附录1
  template: '<div></div>',
  mounted() {
    console.log(this.param1);  // 值1
    console.log(this.param2);  // undefined

    console.log(this.$attrs); // 返回父作用域中非 prop 的特性 (class 和 style 除外)的对象集合
    console.log(this.$attrs.param1);  // undefined
    console.log(this.$attrs.param2);  // 值2

    console.log(this.$listeners);  // 返回父作用域中的 (不含 .native 修饰器的) v-on 事件集合
    console.log(this.$listeners.event1);  // fn1
    console.log(this.$listeners.event2);  // undefined
    this.$emit('event1');
  }
})
<test-prop 
  param1="值1" 
  param2="值2" 
  v-on:event1="fn1" 
  v-on:event2.native="fn2"></test-prop>
<!-- 此时 传入的param2 即为 非 prop 的特性 -->

通过 $attrs$listeners 属性可以降低在不使用Vuex以及事件总线的情况下,组件跨级props以及事件传递的复杂度。

<!-- 组件A -->
<template>
    <div id="app">
        <B :propB="值" type="password" @test1="onTest1" @test2="onTest2"></B>
    </div>
</template>
<script>
    export default {
        components: { B },
        methods: {
            onTest1() { console.log('我是A组件的方法Test1'); },
            onTest2() { console.log('我是A组件的方法Test2'); }
        }
    };
</script>

<!-- 组件B -->
<template>
    <div>
        <!-- 中间件,将type传入C组件,将事件onTest1、onTest2传递 -->
        <C v-bind="$attrs" v-on="$listeners"></C>
    </div>
</template>
<script>
    export default {
        props: ['propB'],
        inheritAttrs: false,
        components: { C },
        mounted() {
            this.$emit('test1');  // 执行 test1
        }
    };
</script>

<!-- 组件C -->
<template>
    <!-- 此时若设置 inheritAttrs 为 true,会导致该input框的type值为password -->
    <input type="text">
</template>
<script>
    export default {
        props: ['child2'],
        inheritAttrs: false,
        mounted() {
            this.$emit('test2');  // 执行 test2
        }
    };
</script>

附录1:
inheritAttrs
默认情况下父作用域的非 prop 的特性会 挂载到 根元素(属性)上,通过设置 inheritAttrsfalse可去除。
注意:这个选项不影响 classstyle 绑定。

<div param2="值2"></div>
将 `inheritAttrs` 设为 `false` 后 
<div></div>

摘录于:Vue官网

相关文章

  • 非 Prop 的特性

    一个非 prop 特性(属性)是指传向一个组件,但是该组件并没有相应 prop 定义的特性。 通过 $attrs ...

  • Vue之prop

    这里暂时仅介绍下“非prop特性(attr)”一般的特性是显式定义的(即 prop特性),如下面代码中的 post...

  • Vue组件中如何获取非Prop特性的值

    学习Vue.js文档的过程中,发现组件那一章,非Prop特性一节有些疑问: 所谓非 prop 特性,就是指它可以直...

  • vue 组件 - prop

    prop 是什么 prop 是你可以在组件上注册的一些自定义特性。当一个值传递给一个 prop 特性的时候,它就变...

  • vue组件参数校验和非prop特性

    1.父组件再给子组件传值的时候,子组件接收值,子组件会对接收的值进行一些规范约束,主要一下几种方式: 2.非pro...

  • Vue学习随笔,待整理

    1.非 Prop 的特性会添加到子组件的根元素上。 2. 传入type会覆盖,e.x

  • YYLabel

    YYLabel 继承View,功能更加强大,支持所有UILabel的特性 1.可以实现垂直文字文字布局 @prop...

  • 跨组件属性传递

    Vue $attrs 包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 styl...

  • 10-Vue之Prop

    Prop 的大小写 (camelCase vs kebab-case) HTML 中的特性名是大小写不敏感的,所以...

  • vue组件三大核心概念

    属性 自定义属性propsProp 可以在组件上注册一些自定义特性。当一个值传递给一个 prop 特性的时候,它就...

网友评论

    本文标题:非 Prop 的特性

    本文链接:https://www.haomeiwen.com/subject/gfbpdftx.html