You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're showing you a simple example here, but in a typical Vue application we use Single File Components instead of a string template. You can find more information about them [in this section](single-file-component.html).
26
+
We're showing you a simple example here, but in a typical Vue application we use Single File Components instead of a string template.
Unlike components and props, event names don't provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event. For example, if emitting a camelCased event name:
7
+
# #事件名称
8
+
Unlike components and props, event names don't provide any automatic case transformation.
9
+
与组件和道具不同,事件名不提供任何自动案例转换。
10
+
Instead, the name of an emitted event must exactly match the name used to listen to that event.
11
+
相反,所发出事件的名称必须与用于侦听该事件的名称完全匹配。
12
+
For example, if emitting a camelCased event name:
13
+
例如,如果发送驼峰大小写事件名:
8
14
9
15
```js
10
16
this.$emit('myEvent')
11
17
```
12
-
13
18
Listening to the kebab-cased version will have no effect:
Since event names will never be used as variable or property names in JavaScript, there is no reason to use camelCase or PascalCase. Additionally, `v-on` event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML's case-insensitivity), so `@myEvent` would become `@myevent` -- making `myEvent` impossible to listen to.
26
+
Since event names will never be used as variable or property names in JavaScript, there is no reason to use camelCase or PascalCase.
Additionally, `v-on` event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML's case-insensitivity), so `@myEvent` would become `@myevent` -- making `myEvent` impossible to listen to
For these reasons, we recommend you **always use kebab-case for event names**.
23
-
32
+
由于这些原因,我们建议您**始终对事件名称使用(短横线隔开式)**。
24
33
## Defining Custom Events
25
34
26
35
<VideoLessonhref="https://vueschool.io/lessons/defining-custom-events-emits?friend=vuejs"title="Learn how to define which events a component can emit with Vue School">Watch a free video about Defining Custom Events on Vue School</VideoLesson>
27
36
28
37
Emitted events can be defined on the component via the `emits` option.
It is recommended to define all emitted events in order to better document how a component should work.
51
+
建议定义所有发出的事件,以便更好地记录组件应该如何工作。
52
+
:::
40
53
:::
41
-
42
54
### Validate Emitted Events
43
-
55
+
###验证发出的事件
44
56
Similar to prop type validation, an emitted event can be validated if it is defined with the Object syntax instead of the Array syntax.
45
-
57
+
与prop类型验证类似,如果使用对象语法而不是数组语法定义发出的事件,则可以对其进行验证。
46
58
To add validation, the event is assigned a function that receives the arguments passed to the `$emit` call and returns a boolean to indicate whether the event is valid or not.
Usually though, you'll want every prop to be a specific type of value. In these cases, you can list props as an object, where the properties' names and values contain the prop names and types, respectively:
14
+
Usually though, you'll want every prop to be a specific type of value.
15
+
通常情况下,你会希望每个props都有特定的价值类型。
16
+
In these cases, you can list props as an object, where the properties' names and values contain the prop names and types, respectively:
This not only documents your component, but will also warn users in the browser's JavaScript console if they pass the wrong type. You'll learn much more about [type checks and other prop validations](#prop-validation) further down this page.
28
-
31
+
This not only documents your component, but will also warn users in the browser's JavaScript console if they pass the wrong type.
So far, you've seen props passed a static value, like in:
38
+
到目前为止,您已经看到了props传递静态值,如:
32
39
33
40
```html
34
41
<blog-posttitle="My journey with Vue"></blog-post>
35
42
```
36
43
37
44
You've also seen props assigned dynamically with `v-bind` or its shortcut, the `:` character, such as in:
45
+
你也看到了使用“v-bind”或它的快捷方式“:”字符来动态分配props,例如:
38
46
39
47
```html
40
48
<!-- Dynamically assign the value of a variable -->
@@ -45,6 +53,7 @@ You've also seen props assigned dynamically with `v-bind` or its shortcut, the `
45
53
```
46
54
47
55
In the two examples above, we happen to pass string values, but _any_ type of value can actually be passed to a prop.
56
+
在上面的两个示例中,我们碰巧传递了字符串值,但是_any_类型的值实际上可以传递给prop。
48
57
49
58
### Passing a Number
50
59
@@ -99,8 +108,11 @@ In the two examples above, we happen to pass string values, but _any_ type of va
99
108
```
100
109
101
110
### Passing the Properties of an Object
102
-
103
-
If you want to pass all the properties of an object as props, you can use `v-bind` without an argument (`v-bind` instead of `:prop-name`). For example, given a `post` object:
111
+
传递对象的属性
112
+
If you want to pass all the properties of an object as props, you can use `v-bind` without an argument (`v-bind` instead of `:prop-name`).
All props form a **one-way-down binding** between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent's state, which can make your app's data flow harder to understand.
127
-
128
-
In addition, every time the parent component is updated, all props in the child component will be refreshed with the latest value. This means you should **not** attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console.
129
-
137
+
单向数据流
138
+
All props form a **one-way-down binding** between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around.
This prevents child components from accidentally mutating the parent's state, which can make your app's data flow harder to understand.
141
+
这可以防止子组件意外地改变父组件的状态,这会使应用程序的数据流更难理解。
142
+
In addition, every time the parent component is updated, all props in the child component will be refreshed with the latest value.
143
+
此外,每次更新父组件时,子组件中的所有props都将使用最新值刷新。
144
+
This means you should **not** attempt to mutate a prop inside a child component.
145
+
这意味着您不应该尝试在子组件内修改prop。
146
+
If you do, Vue will warn you in the console.
147
+
如果您这样做,Vue将在控制台中警告您。
130
148
There are usually two cases where it's tempting to mutate a prop:
131
-
132
-
1.**The prop is used to pass in an initial value; the child component wants to use it as a local data property afterwards.** In this case, it's best to define a local data property that uses the prop as its initial value:
149
+
通常有两种情况下,它是诱人的变异一种props:
150
+
1.
151
+
1.
152
+
**The prop is used to pass in an initial value;
153
+
**使用prop传递一个初始值;
154
+
the child component wants to use it as a local data property afterwards.
155
+
子组件希望之后将其用作本地数据属性。
156
+
** In this case, it's best to define a local data property that uses the prop as its initial value:
157
+
**在这种情况下,最好定义一个使用prop作为初始值的本地数据属性:
133
158
134
159
```js
135
160
props: ['initialCounter'],
@@ -140,7 +165,10 @@ data() {
140
165
}
141
166
```
142
167
143
-
2.**The prop is passed in as a raw value that needs to be transformed.** In this case, it's best to define a computed property using the prop's value:
168
+
**The prop is passed in as a raw value that needs to be transformed.
169
+
** prop作为需要转换的原始值传入。
170
+
** In this case, it's best to define a computed property using the prop's value:
171
+
**在这种情况下,最好使用props的值定义一个计算属性:
144
172
145
173
```js
146
174
props: ['size'],
@@ -152,15 +180,23 @@ computed: {
152
180
```
153
181
154
182
::: tip Note
183
+
:::提示注意
155
184
Note that objects and arrays in JavaScript are passed by reference, so if the prop is an array or object, mutating the object or array itself inside the child component **will** affect parent state.
Components can specify requirements for their props, such as the types you've already seen. If a requirement isn't met, Vue will warn you in the browser's JavaScript console. This is especially useful when developing a component that's intended to be used by others.
161
-
162
-
To specify prop validations, you can provide an object with validation requirements to the value of `props`, instead of an array of strings. For example:
163
-
189
+
# #支持验证
190
+
Components can specify requirements for their props, such as the types you've already seen.
191
+
组件可以为它们的props指定需求,比如您已经看到的类型。
192
+
If a requirement isn't met, Vue will warn you in the browser's JavaScript console.
193
+
如果没有满足某个要求,Vue将在浏览器的JavaScript控制台警告您。
194
+
This is especially useful when developing a component that's intended to be used by others.
195
+
这在开发供他人使用的组件时特别有用。
196
+
To specify prop validations, you can provide an object with validation requirements to the value of `props`, instead of an array of strings.
When prop validation fails, Vue will produce a console warning (if using the development build).
210
-
246
+
当props验证失败时,Vue将生成一个控制台警告(如果使用的是开发版本)。
211
247
::: tip Note
212
-
Note that props are validated **before** a component instance is created, so instance properties (e.g. `data`, `computed`, etc) will not be available inside `default` or `validator` functions.
248
+
:::提示注意
249
+
Note that props are validated **before** a component instance is created, so instance properties (e.g.
250
+
注意,props是在创建组件实例之前**验证的,所以实例属性(例如。
251
+
`data`, `computed`, etc) will not be available inside `default` or `validator` functions.
252
+
' data ', ' computed '等)在' default '或' validator '函数中不可用。
253
+
:::
213
254
:::
214
255
215
256
### Type Checks
@@ -225,8 +266,10 @@ The `type` can be one of the following native constructors:
225
266
- Function
226
267
- Symbol
227
268
228
-
In addition, `type` can also be a custom constructor function and the assertion will be made with an `instanceof` check. For example, given the following constructor function exists:
229
-
269
+
In addition, `type` can also be a custom constructor function and the assertion will be made with an `instanceof` check.
270
+
此外,‘type’也可以是一个自定义构造函数,断言将通过‘instanceof’检查完成。
271
+
For example, given the following constructor function exists:
272
+
例如,给定以下构造函数存在:
230
273
```js
231
274
functionPerson(firstName, lastName) {
232
275
this.firstName= firstName
@@ -248,7 +291,10 @@ to validate that the value of the `author` prop was created with `new Person`.
248
291
249
292
## Prop Casing (camelCase vs kebab-case)
250
293
251
-
HTML attribute names are case-insensitive, so browsers will interpret any uppercase characters as lowercase. That means when you're using in-DOM templates, camelCased prop names need to use their kebab-cased (hyphen-delimited) equivalents:
294
+
HTML attribute names are case-insensitive, so browsers will interpret any uppercase characters as lowercase.
295
+
HTML属性名不区分大小写,因此浏览器将任何大写字符解释为小写字符。
296
+
That means when you're using in-DOM templates, camelCased prop names need to use their kebab-cased (hyphen-delimited) equivalents:
0 commit comments