vue.js 组件需要 name 选项吗?

HWo*_*o_K 6 vue.js

如果我不在组件中设置名称,是否会导致任何问题?
即使我不设置它,我也可以使用它。

    <template>
      <div> some component</div>
    </template>
    
    <script>
    export default {
        name: "SomeComponent" // is this essential?
    };
    </script>
Run Code Online (Sandbox Code Playgroud)

Sph*_*inx 5

注册一个组件时,需要它的ID,但不需要Name

正如Vue 指南:Vue.component所说:

注册还会自动使用给定的 id 设置组件的名称。

因此,在声明一个组件时,将 component.name 留空就可以了。

但最好的做法是始终为您的组件命名。

因为(参考:Vue 指南:Vue.component options):

允许组件在其模板中递归调用自身。请注意,当使用 Vue.component() 全局注册组件时,全局 ID 会自动设置为其名称。

指定名称选项的另一个好处是调试。命名的组件会产生更有用的警告消息。此外,在 vue-devtools 中检查应用程序时,未命名的组件将显示为 ,这不是很有用。通过提供 name 选项,您将获得更多信息的组件树。

  • 我还没有看到“&lt;AnonymousComponent&gt;”出现,如上所述。如果我不指定组件的名称,开发工具将使用它在其父组件中注册时使用的名称。 (2认同)