Copying a vue component. - Undefined #11542
Replies: 1 comment 1 reply
-
This is not related to Vue components or anything, you just have a misunderstanding about how Javascript variable declarations work. You wanted to assign a value to the aforedeclared let variable Solution: remove the const authStore = useAuthStore();
let MyMenuConfig;
if (authStore.isAuthenticated) {
- const MyMenuConfig = ref(AuthMenuConfig);
+ MyMenuConfig = ref(AuthMenuConfig);
} else {
- const MyMenuConfig = ref(MainMenuConfig);
+ MyMenuConfig = ref(MainMenuConfig);
}
console.log("MYMENUCONFIG:" + MyMenuConfig); here's an article I quickly googled that might help you better understan: https://dev.to/catherineisonline/what-is-variable-shadowing-in-javascript-59ci Something Vue-specific that I would further improve is to use one ref for the const authStore = useAuthStore();
- let MyMenuConfig;
+ const MyMenuConfig = ref();
if (authStore.isAuthenticated) {
- const MyMenuConfig = ref(AuthMenuConfig);
+ MyMenuConfig.value = AuthMenuConfig;
} else {
- const MyMenuConfig = ref(MainMenuConfig);
+ MyMenuConfig.value = MainMenuConfig;
}
- console.log("MYMENUCONFIG:" + MyMenuConfig);
+ console.log("MYMENUCONFIG:" + MyMenuConfig.value); |
Beta Was this translation helpful? Give feedback.
-
Noob here so not even sure I am asking the right question.
Basically in the below code I want to copy one component to another based on if user logged in.
AuthMenuConfig & Main MenuConfig are both typescript files that have been imported.
The console.log shows that MyMenuConfig is always undefined. What dumb thing am I doing?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions