非 Prop 的 Attribute

一个非 prop 的 attribute 是指传向一个组件,但是该组件并没有相应 props 或 emits 定义的 attribute。常见的示例包括 class、style 和 id attribute。可以通过 $attrs property 访问那些 attribute。
(1)Attribute 继承
当组件返回单个根节点时,非 prop 的 attribute 将自动添加到根节点的 attribute 中。例如,在 date-picker 组件的实例中:

app.component('date-picker', {
  template: `
    <div class="date-picker">
      <input type="datetime-local" />
    </div>
  `
})

如果我们需要通过 data-status attribute 定义 组件的状态,它将应用于根节点 (即 div.date-picker)。

<!-- 具有非 prop 的 attribute 的 date-picker 组件-->
<date-picker data-status="activated"></date-picker>
<!-- 渲染后的 date-picker 组件 -->
<div class="date-picker" data-status="activated">
  <input type="datetime-local" />
</div>

同样的规则也适用于事件监听器:

<date-picker @change="submitChange"></date-picker>
app.component('date-picker', {
  created() {
    console.log(this.$attrs) // { onChange: () => {}  }
  }
})

当一个具有 change 事件的 HTML 元素作为 date-picker 的根元素时,这可能会有帮助。

app.component('date-picker', {
  template: `
    <select>
      <option value="1">Yesterday</option>
      <option value="2">Today</option>
      <option value="3">Tomorrow</option>
    </select>
  `
})

在这种情况下,change 事件监听器将从父组件传递到子组件,它将在原生 的 change 事件上触发。我们不需要显式地从 date-picker 发出事件:

<div id="date-picker" class="demo">
  <date-picker @change="showChange"></date-picker>
</div>
const app = Vue.createApp({
  methods: {
    showChange(event) {
      console.log(event.target.value) // 将打印所选选项的值
    }
  }
})

(2)禁用 Attribute 继承
如果你不希望组件的根元素继承 attribute,可以在组件的选项中设置 inheritAttrs: false

禁用 attribute 继承的常见场景是需要将 attribute 应用于根节点之外的其他元素。

通过将 inheritAttrs 选项设置为 false,你可以使用组件的 $attrs property 将 attribute 应用到其它元素上,该 property 包括组件 props 和 emits property 中未包含的所有属性 (例如,class、style、v-on 监听器等)。

使用上一节中的 date-picker 组件示例,如果需要将所有非 prop 的 attribute 应用于 input 元素而不是根 div 元素,可以使用 v-bind 缩写来完成。

app.component('date-picker', {
  inheritAttrs: false,
  template: `
    <div class="date-picker">
      <input type="datetime-local" v-bind="$attrs" />
    </div>
  `
})

有了这个新配置,data-status attribute 将应用于 input 元素!

<!-- date-picker 组件使用非 prop 的 attribute -->
<date-picker data-status="activated"></date-picker>

<!-- 渲染后的 date-picker 组件 -->
<div class="date-picker">
  <input type="datetime-local" data-status="activated" />
</div>

(3)多个根节点上的 Attribute 继承
与单个根节点组件不同,具有多个根节点的组件不具有自动 attribute fallthrough (隐式贯穿) 行为。如果未显式绑定 $attrs,将发出运行时警告

<custom-layout id="custom-layout" @click="changeValue"></custom-layout>
// 这将发出警告
app.component('custom-layout', {
  template: `
    <header>...</header>
    <main>...</main>
    <footer>...</footer>
  `
})
// 没有警告,$attrs 被传递到 <main> 元素
app.component('custom-layout', {
  template: `
    <header>...</header>
    <main v-bind="$attrs">...</main>
    <footer>...</footer>
  `
})
The error message "AttributeError: 'bpy_prop_collection' object has no attribute 'bezier_points'" in the Blender context typically occurs when you're trying to access an attribute or method that doesn't exist in the object you're working with. In this case, it seems you are referring to a `bpy_prop_collection` type, which is a built-in Blender data structure used for collections of properties, and specifically looking for a 'bezier points' attribute. Beziers are commonly associated with Bézier curves or spline geometry in Blender, but a `bpy_prop_collection` does not directly have a 'bezier_points' attribute. It might be the case that the expected attribute is part of a specific type of object within the collection, like a `Curve` or `BezierCurve` object, which do have Bezier points. To resolve the issue, make sure you are targeting the correct object instance and checking if the 'bezier points' attribute is indeed available on that type. Here are some steps to debug: 1. Verify the object: Ensure you have a `Curve` or `BezierCurve` object, and not just any `bpy_prop_collection`. 2. Check documentation: Consult the Blender API documentation to confirm if the 'bezier_points' attribute is relevant to the object's class. 3. Use hasattr() function: Before accessing the attribute, use `hasattr()` function to check if it exists, like `if hasattr(obj, 'bezier_points')`. 4. Handle the AttributeError: Wrap your code in a try-except block to catch the error gracefully and provide alternative logic if the attribute is not found. Here's an example of how you might use the `hasattr()` function: ```python if hasattr(obj, 'bezier_points'): # Access the bezier points attribute here points = obj.bezier_points else: print(f"The {obj} object does not have a 'bezier_points' attribute.") ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值