Vue.js 中的美元前缀 ($) 是什么意思? [英] What does the dollar prefix ($) mean in Vue.js?
问题描述
Vue.js 中属性名称前的美元字符/符号前缀是什么意思?
What is the meaning of the dollar character/symbol prefix before property names in Vue.js?
例如:this.$emit('clicked', 'demo')
推荐答案
$
和 _
前缀在 Vue 中的使用解释如下:
The use of the $
and _
prefixes in Vue are explained here:
https://vuejs.org/v2/style-guide/#Private-property-names-essential
特别是在详细说明部分.
_
用于私有实例属性:
Vue 使用 _ 前缀来定义自己的私有属性...
Vue uses the _ prefix to define its own private properties...
$
用于公共实例属性:
至于 $ 前缀,它在 Vue 生态系统中的目的是暴露给用户的特殊实例属性...
As for the $ prefix, its purpose within the Vue ecosystem is special instance properties that are exposed to the user...
两者都用于避免与组件创建者选择的属性名称发生冲突,例如 props 和 data 属性.
Both are used to avoid collisions with property names chosen by component creators, such as props and data properties.
$
前缀不仅被 Vue 的核心 API 使用.它也常被用于向组件添加属性的库使用.例如:
The $
prefix is not only used by Vue's core APIs. It's also commonly used by libraries that add properties to components. e.g.:
- Vuex 添加了
$store
. - Vue Router 添加了
$route
和$router
.
这些都是官方支持的库,但许多第三方库也是如此.
These are both officially supported libraries but the same is true of many third-party libraries.
它也可以由创建全局属性的应用程序代码使用.一个常见的例子是将 $http
添加到 Vue.prototype
(或 Vue 3 中的 globalProperties
).
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).
在所有这些情况下,$
都向未来的开发人员表明某个属性是在别处定义的,而不是在当前组件中定义的.
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.
这篇关于Vue.js 中的美元前缀 ($) 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!