Skip to content

Commit 9191673

Browse files
committed
relaxed filter usage
1 parent cc875c0 commit 9191673

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/v2/guide/syntax.md

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,6 @@ These expressions will be evaluated as JavaScript in the data scope of the owner
8080

8181
<p class="tip">Template expressions are sandboxed and only have access to a whitelist of globals such as `Math` and `Date`. You should not attempt to access user defined globals in template expressions.</p>
8282

83-
### Filters
84-
85-
Vue.js allows you to define filters that can be used to apply common text formatting. Filters should be appended to the end of a **mustache interpolation**, denoted by the "pipe" symbol:
86-
87-
``` html
88-
{{ message | capitalize }}
89-
```
90-
91-
<p class="tip">Vue 2.x filters can only be used inside mustache bindings. To achieve the same behavior inside directive bindings, you should use [Computed properties](computed.html) instead.</p>
92-
93-
The filter function always receives the expression's value as the first argument.
94-
95-
``` js
96-
new Vue({
97-
// ...
98-
filters: {
99-
capitalize: function (value) {
100-
if (!value) return ''
101-
value = value.toString()
102-
return value.charAt(0).toUpperCase() + value.slice(1)
103-
}
104-
}
105-
})
106-
```
107-
108-
Filters can be chained:
109-
110-
``` html
111-
{{ message | filterA | filterB }}
112-
```
113-
114-
Filters are JavaScript functions, therefore they can take arguments:
115-
116-
``` html
117-
{{ message | filterA('arg1', arg2) }}
118-
```
119-
120-
Here, the plain string `'arg1'` will be passed into the filter as the second argument, and the value of expression `arg2` will be evaluated and passed in as the third argument.
121-
12283
## Directives
12384

12485
Directives are special attributes with the `v-` prefix. Directive attribute values are expected to be **a single JavaScript expression** (with the exception for `v-for`, which will be discussed later). A directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Let's review the example we saw in the introduction:
@@ -157,6 +118,49 @@ Modifiers are special postfixes denoted by a dot, which indicate that a directiv
157118

158119
We will see more use of modifiers later when we take a more thorough look at `v-on` and `v-model`.
159120

121+
## Filters
122+
123+
Vue.js allows you to define filters that can be used to apply common text formatting. Filters are usable in two places: **mustache interpolations and `v-bind` expressions**. Filters should be appended to the end of the JavaScript expression, denoted by the "pipe" symbol:
124+
125+
``` html
126+
<!-- in mustaches -->
127+
{{ message | capitalize }}
128+
129+
<!-- in v-bind -->
130+
<div v-bind:id="rawId | formatId"></div>
131+
```
132+
133+
<p class="tip">Vue 2.x filters can only be used inside mustache interpolations and `v-bind` expressions (the latter supported since 2.1.0), because filters are primarily designed for text transformation purposes. For more complex data transforms in other directives, you should use [Computed properties](computed.html) instead.</p>
134+
135+
The filter function always receives the expression's value as the first argument.
136+
137+
``` js
138+
new Vue({
139+
// ...
140+
filters: {
141+
capitalize: function (value) {
142+
if (!value) return ''
143+
value = value.toString()
144+
return value.charAt(0).toUpperCase() + value.slice(1)
145+
}
146+
}
147+
})
148+
```
149+
150+
Filters can be chained:
151+
152+
``` html
153+
{{ message | filterA | filterB }}
154+
```
155+
156+
Filters are JavaScript functions, therefore they can take arguments:
157+
158+
``` html
159+
{{ message | filterA('arg1', arg2) }}
160+
```
161+
162+
Here, the plain string `'arg1'` will be passed into the filter as the second argument, and the value of expression `arg2` will be evaluated and passed in as the third argument.
163+
160164
## Shorthands
161165

162166
The `v-` prefix serves as a visual cue for identifying Vue-specific attributes in your templates. This is useful when you are using Vue.js to apply dynamic behavior to some existing markup, but can feel verbose for some frequently used directives. At the same time, the need for the `v-` prefix becomes less important when you are building an [SPA](https://en.wikipedia.org/wiki/Single-page_application) where Vue.js manages every template. Therefore, Vue.js provides special shorthands for two of the most often used directives, `v-bind` and `v-on`:

0 commit comments

Comments
 (0)