have developed a laravel intertia vue project to create dashbaord using th following method:
composer require laraveldaily/larastarters --dev
php artisan larastarters:install
Selected option 1, Laravel Breeze & Inertia (Tailwind)
Now I pass a shared variable sharedata in HandleInertiaRequests
and pass it in the return object as:
public function share(Request $request)
{
return array_merge(parent::share($request), [
'auth' => [
'user' => $request->user(),
],
'sharedata'=> "Test data",
'flash' => function () use ($request) {
return [
'success' => $request->session()->get('success'),
];
},
'showingMobileMenu' => false,
]);
}
Now i try to obtain the sharedata as props in my autogenerated Layouts/Navigation.vue
and try to console log the sharedata when the navbar is mounted. Following is my updated
Navigation.vue.
<template>
... content of navbar
</template>
<script>
import { onMounted } from 'vue';
import NavLink from '@/Components/NavLink.vue';
import { Link } from '@inertiajs/vue3';
import { ref } from 'vue'
export default {
components: {
NavLink,
Link,
},
props: {
sharedata: String
},
setup() {
let showingTwoLevelMenu = ref(false);
onMounted(() => {
console.log('Component mounted with sharedata:', this.sharedata);
});
return {
showingTwoLevelMenu
}
},
}
</script>
I get error that i cannot use this in the this.sharedata. Where is the problem in my code? Please provide help. Thanks in advance
3
Answers
I updated the code like below and it now works
The error come from that you’re trying to use this in the setup() function in Vue 3’s Composition API.
You should access the sharedata prop directly from the props argument. So, your updated setup() function should look like this:
I usually work with script setup and to access the shared data I have to use the usePgae() hook like this
other then that you can also try this