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
Directive arguments can be dynamic. For example, in `v-mydirective:[argument]="value"`, the `argument` can be updated based on data properties in our component instance! This makes our custom directives flexible for use throughout our application.
Let's say you want to make a custom directive that allows you to pin elements to your page using fixed positioning. We could create a custom directive where the value updates the vertical positioning in pixels, like this:
```html
<divid="baseexample">
<p>Scroll down the page</p>
<pv-pin="200">Stick me 200px from the top of the page</p>
</div>
```
```js
Vue.directive('pin', {
bind:function (el, binding, vnode) {
el.style.position='fixed'
el.style.top=binding.value+'px'
}
})
newVue({
el:'#baseexample'
})
```
This would pin the element 200px from the top of the page. But what happens if we run into a scenario when we need to pin the element from the left, instead of the top? Here's where a dynamic argument that can be updated per component instance comes in very handy:
```html
<divid="dynamicexample">
<h3>Scroll down inside this section ↓</h3>
<pv-pin:[direction]="200">I am pinned onto the page at 200px to the left.</p>
kr.vuejs.org/src/v2/guide/custom-directive.md
Lines 145 to 209 in 6df59ee
The text was updated successfully, but these errors were encountered: