首页 > 解决方案 > How to specify "key" as a required prop?

问题描述

I want this

export default {

  props: {
    key: {
      type: String,
      required: true,
    },
  },
  ...    
}

But it leads to a runtime error:

[Vue warn]: "key" is a reserved attribute and cannot be used as component prop.

EDIT:

To clarify: I want the reserved attribute "key" to be required. This is because my component relies on the trick of "Forcing component recreation by key change". (link)

标签: vue.jsvuejs2

解决方案


key是Vue.js 保留的特殊属性之一。

它不能作为, 和, , ,prop一样传递。refslotscoped-slotis

只需将 重命名prop为您选择的任何名称。

如果命名属性的使用key在子组件内至关重要,则可以key在子组件内创建一个计算属性,该属性将从传递的值返回,prop并可在子组件内访问。


推荐阅读