Vue.js 中的美元前缀 ($) 是什么意思?

What does the dollar prefix ($) mean in Vue.js?

Vue.js 中 属性 名称前的美元 character/symbol 前缀是什么意思?

例如:this.$emit('clicked', 'demo')

Vue中$_前缀的使用说明如下:

https://vuejs.org/v2/style-guide/#Private-property-names-essential

具体在详细说明部分。

_ 用于私有实例属性:

Vue uses the _ prefix to define its own private properties...

$ 用于 public 实例属性:

As for the $ prefix, its purpose within the Vue ecosystem is special instance properties that are exposed to the user...

两者都用于避免与组件创建者选择的 属性 名称发生冲突,例如道具和数据属性。


$ 前缀不仅被 Vue 的核心 API 使用。它也常被用于向组件添加属性的库使用。例如:

  • Vuex 添加 $store.
  • Vue Router 添加 $route$router.

这些都是官方支持的库,但许多 third-party 库也是如此。

创建全局属性的应用程序代码也可以使用它。一个常见的例子是将 $http 添加到 Vue.prototype(或 Vue 3 中的 globalProperties)。

在所有这些情况下,$ 向未来的开发人员表明 属性 是在其他地方而不是在当前组件中定义的。