I've encountered a peculiar issue with Vue (v2.6.14) where I'm trying to create a new array based on one received as a prop. Here's the code snippet that's causing the trouble:
props: { employees: Array },
data() {
return {
sortedEmployees: [],
};
},
mounted() {
this.sortedEmployees = this.employees.slice(0);
},
The main goal here is to generate a separate reference for the employees array so that I can sort it for later display without modifying the original array. I don't need to clone the actual objects inside the array since I won't be changing them.
The issue arises when the app 'hot reloads' due to a code change – everything works perfectly, the hook is triggered and the component data is set correctly. However, when I refresh the page in the browser, even though the hook is still executed, the component data isn't properly set, resulting in an empty array. One way to resolve this is by setting up a watcher for the prop to ensure the data is properly updated, but my focus here is on understanding why this discrepancy occurs. If the hook is being called during a page refresh, why doesn't it set the data as expected, like it does during 'hot reloading'?
I'm working with a simple setup created using vue-cli without any complex configurations. Any insights into what might be causing this behavior would be greatly appreciated.