简体   繁体   English

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

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

What is the meaning of the dollar character/symbol prefix before property names in Vue.js? Vue.js 中属性名称前的美元字符/符号前缀是什么意思?

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

The use of the $ and _ prefixes in Vue are explained here: Vue 中$_前缀的使用说明如下:

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

Specifically in the Detailed Explanation section.具体在详细说明部分。

_ is for private instance properties: _用于私有实例属性:

Vue uses the _ prefix to define its own private properties... Vue 使用 _ 前缀来定义自己的私有属性...

$ is for public instance properties: $用于公共实例属性:

As for the $ prefix, its purpose within the Vue ecosystem is special instance properties that are exposed to the user...至于 $ 前缀,它在 Vue 生态系统中的用途是向用户公开的特殊实例属性......

Both are used to avoid collisions with property names chosen by component creators, such as props and data properties.两者都用于避免与组件创建者选择的属性名称发生冲突,例如 props 和 data 属性。


The $ prefix is not only used by Vue's core APIs. $前缀不仅被 Vue 的核心 API 使用。 It's also commonly used by libraries that add properties to components.向组件添加属性的库也经常使用它。 eg:例如:

  • Vuex adds $store . Vuex 添加了$store
  • Vue Router adds $route and $router . Vue Router 添加了$route$router

These are both officially supported libraries but the same is true of many third-party libraries.这些都是官方支持的库,但许多第三方库也是如此。

It can also be used by application code that creates global properties.创建全局属性的应用程序代码也可以使用它。 A common example is adding $http to Vue.prototype (or globalProperties in Vue 3).一个常见的例子是将$http添加到Vue.prototype (或 Vue 3 中的globalProperties )。

In all of these cases, the $ acts as an indicator to future developers that a property is defined elsewhere and not within the current component.在所有这些情况下, $充当了未来开发人员的指示符,表明属性是在其他地方定义的,而不是在当前组件中定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM