I have a UI-template node which values are contained in a data array as below.
data() {
return{
values:[]
}
}
I want to initialize the values of this array with the data coming from msg.payload.
I tried using the mounted function with no success.
How would you do that ?
You can't initialise it with a value from msg.payload as it is initialised when the widget is loaded. You can update it when a message comes in by providing a watch msg function in the script. Something like
That assumes the values array is in msg.payload. Adjust according to where the values are. You might want to have some validation code to make sure the payload is valid before updating the data.
Awesome ! I have implemented your solution with minor adaptation to initialize the value only upon the first message received (using msgCount flag) and it answer my needs.
<script>
export default {
data() {
// define variables available component-wide
// (in <template> and component functions)
return {
count: 0,
data:[]
}
},
...
mounted() {
// code here when the component is first loaded
this.data=[4,5,6];
},
...
Is that any different to just doing
<script>
export default {
data() {
// define variables available component-wide
// (in <template> and component functions)
return {
count: 0,
data:[4,5,6]
}
},