将组件指定为标记或属性的区别

Difference in specifying component as tag or as an attribute

假设有一个 Angular 分量 selector: 'my-component'。 两种方式都可以:

<div><my-component/></div>

<div my-component></div>

渲染或组件行为有什么不同吗?如果不是那么为什么我们有 'attribute' 选项?

selector: 'my-component' 恰恰意味着您要为组件使用 my-component 元素。

如果要使用属性,请使用 [my-component]

如果您想同时使用两者,请使用 my-component,[my-component]

选择器是一个 CSS 选择器,就像您将在 CSS 文件中使用的 CSS 选择器一样。它们受到更多限制,但语法相同。