question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom directive binds differs on components vs elements

See original GitHub issue

Version

2.6.10

Reproduction link

https://jsfiddle.net/bponomarenko/uom10qd2/

Steps to reproduce

  1. Open browser console.
  2. Click on Toggle button two times.

What is expected?

Directive will emit the same console messages when applied to DOM elements and to components (on init and after click on button).

I’m not sure what should be expected output. Either

bind: first comp
bind: first elem
unbind: first comp
unbind: first elem
bind: first comp
bind: first elem

or

bind: first comp
bind: first elem
unbind: second comp
unbind: second elem
bind: first comp
bind: first elem

What is actually happening?

Messages from the directive are the same on init, but different after button have been clicked.

Actual console output:

bind: first comp
bind: first elem
unbind: first comp
unbind: second elem
bind: second comp
bind: first elem

It seems that order in which directives are applied to DOM elements and components are different. In my setup I have custom directive which relies on some DOM attribute with configuration data. When this custom directive is bound/unbound in a “regular flow” – everything works as expected (directive binds after element attributes are updated). However when directive is bound/unbound in case of Vue “in-place patch strategey”, behavior seems to be different.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
posvacommented, Apr 15, 2019

Seems like the directive is called with the old component (because it’s reused) but it still has old attrs

As a workaround, set a key on one of the components

2reactions
bponomarenkocommented, Apr 16, 2019

@Justineo I understand the concepts behind an optimization itself. In my situation, custom directive is responsible for adding the state attribute to element on bind and removing that attribute on unbind. And directive is perfectly added/removed when component is re-used by Vue.js. However order of component properties update and directive initialization is different in different moments of component lifetime, which makes it hard to develop custom directives. Indeed additional documentation on these optimization techniques might be helpful, but inconsistent directives lifecycle events are rather a bug to fix in my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between Angular directives vs components - eduCBA
Angular Components have their own view whereas Angular Directives are used to add additional behavior to an existing DOM ...
Read more >
What is the difference between component and directive?
There are three types of directives: Component: A view with associated behaviour. This type of directive actually adds DOM elements. Attribute  ...
Read more >
Difference between Directive and Component in AngularJS
The Component is always an element ('E '). Templates are the mandatory property and always required in Component. The directive can be an ......
Read more >
Angular For Beginners Guide - Components vs Directives
In this article, you will learn how to extend the browser and build your own custom HTML tags, as a means of learning...
Read more >
Building an Angular Custom Directive - Pluralsight
Thus, they are useful to manipulate our DOM by updating certain attributes but do not create or destroy element's as such. Thus, they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found