javascript - Migrating to Vue 2.0 $on() is not hearing $emit() - Stack Overflow
Background: I'm migrating from 1.0 to 2.0.
Currently, I'm using nested ponents. The relationship between the parent and child is perfectly fine. However, the relationship between the parent ponent and the main Vue() instance is broken. In puted property cssstyle
inside the Vue() instance does not get updated based on $emit()/$on() like I was expecting.
The relevant sections:
Usage of parent ponent in HTML:
<f9-padding v-if="editable" ></f9-padding>
Inside the Parent Component
puted: {
cssstyle: function() {
var padding_style = this.padding();
bus.$emit('padding_changed', padding_style);
return padding_style;
}
}
Inside of main Vue() instance
puted: {
cssstyle: function() {
console.log('cssstyle puted');
bus.$on('padding_changed', function (padding_style) {
return padding_style;
});
return 'padding: 0px;';
},
...
The puted property inside the parent updates just fine. However the the puted property inside of the main Vue() instance only runs on page load. I do not understand how to emit data from a parent that is not using an <input>
element. Most of the examples I've found on the site focus solely on the <input>
case. I just have puted property data changing. I tried to use the method show here: .html#Custom-Events, but what concerns me about this documentation is that it is talking about event listening between ponents and not a parent ponent and the main Vue() instance.
Update: I was able to solve this issue by changing the puted property on the parent to this:
puted: {
cssstyle: function() {
var padding_style = this.padding();
this.$root.cssstyle = padding_style;
return padding_style;
}
}
and moving cssstyle from puted to data in the root Vue() instance.
data: {
cssstyle: 'padding: 0px;',
},
However, I feel a bit dirty for using:
this.$root.cssstyle
Is there no other way?
Background: I'm migrating from 1.0 to 2.0.
Currently, I'm using nested ponents. The relationship between the parent and child is perfectly fine. However, the relationship between the parent ponent and the main Vue() instance is broken. In puted property cssstyle
inside the Vue() instance does not get updated based on $emit()/$on() like I was expecting.
The relevant sections:
Usage of parent ponent in HTML:
<f9-padding v-if="editable" ></f9-padding>
Inside the Parent Component
puted: {
cssstyle: function() {
var padding_style = this.padding();
bus.$emit('padding_changed', padding_style);
return padding_style;
}
}
Inside of main Vue() instance
puted: {
cssstyle: function() {
console.log('cssstyle puted');
bus.$on('padding_changed', function (padding_style) {
return padding_style;
});
return 'padding: 0px;';
},
...
The puted property inside the parent updates just fine. However the the puted property inside of the main Vue() instance only runs on page load. I do not understand how to emit data from a parent that is not using an <input>
element. Most of the examples I've found on the site focus solely on the <input>
case. I just have puted property data changing. I tried to use the method show here: http://rc.vuejs/guide/ponents.html#Custom-Events, but what concerns me about this documentation is that it is talking about event listening between ponents and not a parent ponent and the main Vue() instance.
Update: I was able to solve this issue by changing the puted property on the parent to this:
puted: {
cssstyle: function() {
var padding_style = this.padding();
this.$root.cssstyle = padding_style;
return padding_style;
}
}
and moving cssstyle from puted to data in the root Vue() instance.
data: {
cssstyle: 'padding: 0px;',
},
However, I feel a bit dirty for using:
this.$root.cssstyle
Is there no other way?
Share Improve this question edited Jul 9, 2017 at 20:42 Bert 82.5k17 gold badges203 silver badges166 bronze badges asked Sep 4, 2016 at 17:31 nu everestnu everest 10.3k12 gold badges74 silver badges93 bronze badges 2- The first way should work. – gurghet Commented Sep 5, 2016 at 7:45
- The puted cssstyle on the root Vue instance doesn't get triggered by the padding_changed event. – nu everest Commented Sep 5, 2016 at 20:19
1 Answer
Reset to default 5For event emitting and listening check this in the 2.0 docs.
If you emit this
from your custom-ponent
:
cssstyle: function () {
/*...*/
this.$emit('onsomething', params)
}
Then, wherever you make an instance of this ponent, you do something like this
<tamplate>
<custom-ponent @onsomething="action"></custom-ponent>
</tamplate>
Then in your JS:
methods: {
action: function (params) {
console.log('On something')
}
}
- 软件定义存储VS硬件定义存储
- 微软答疑Surface平板:32GB版可用空间20GB
- c++ - Why is my OpenGL application rendering a 3D model with unexpected transparency? - Stack Overflow
- node.js - Parsing error when sending data from godot to api - Stack Overflow
- google apps script - How do I make it so specific cells get deleted or set to a specific # each time the workbook is opened? the
- flutter - having issue about qr code scanner that can scan almost everything - Stack Overflow
- flutter - App Name Not Updating in Android Recent Apps View Despite Manifest and Strings Configuration - Stack Overflow
- maven - Quarkus Live Reload in nested directories - Stack Overflow
- c# - I have a time clock connected to my local network and I have a Windows server on localweb and I can't get the clock
- c++ - GLOG - flags.cc' is being linked both statically and dynamically into this executable - Stack Overflow
- java - JAR works in Command Prompt but not in PHP - Stack Overflow
- pytorch - how to get custom column in the model's forward() function when training with Huggingface Trainer? - Stack Ove
- datagrid - Mudblazor MudDatagrid SelectedItemChanged problem with ondblclick version 8.0.0-preview - Stack Overflow
- flutter - How to write LOGs in an APP made in fletpython, which appear in the DDMS? - Stack Overflow
- c# - Instantiated gameObject in Unity will not update the transform.position - Stack Overflow
- Angular mfe with @module-federationenhanced having issue to use ngx-translate with remote apps - Stack Overflow
- Swift Predicate: Fatal Error Keypaths With Multiple Components - Stack Overflow