John Davidson

javascript - Global Variable getting nulled | VUE JS | Laravel

0 comments
Message:


I need help in checking the issue with the Global variable where it is getting null on assigning it to a local variable.


For Example


A = array('a','b');
let B = A


I am using the B variable but the Global Variable A is getting Empty.


I am attaching the VUE JS script for reference.


Please check if anyone can help. It will be really appreciated.


createBoxes(spec = '') {
let speci = []
if (spec == '') {
speci = this.tabs_specifications
} else {
speci = spec
}
let box = []
for (var j = 1; j <= this.tab_cut; j++) {
for (var i = 0; i < speci.length; i++) {
if (speci[i].tab_position == j) {
box.push(speci[i]);
speci.splice(i, 1)
this.total_boxes = this.total_boxes+1
break;
}
}
}
this.proof_boxes.push(box)
let spicify = speci
if (speci.length > 0) {
this.createBoxes(spicify)
} else {
this.customize_tab.proof_boxes = this.proof_boxes
// window.localStorage.setItem('customizeTab', JSON.stringify(this.$store.state.customize_tab));
}
}


My Answer:
It looks like the issue might be related to how you are assigning the global variable `this.tabs_specifications_` to the local variable `speci`.

In Vue.js, when you assign a global variable to a local variable, it creates a reference to the global variable rather than making a copy of it. This means that any changes made to the local variable will also affect the global variable.

To avoid this issue, you can make a copy of the global variable before assigning it to the local variable. You can do this using the spread operator (`...`) or the `Object.assign()` method.

Here's an example of how you can modify your code to make a copy of the global variable:

javascript
createBoxes(spec = '') {
let speci = [];

if (spec == '') {
speci = [...this.tabs_specifications_];
} else {
speci = spec;
}

let box = [];

for (var j = 1; j < speci.length; j++) {
// Your code here
}
}


By making a copy of `this.tabs_specifications_` using the spread operator `...`, you can ensure that changes made to `speci` do not affect the global variable `this.tabs_specifications_`.

Rate this post

5 of 5 based on 1879 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us