Vue: Cannot convert undefined or null to object #740
-
Ziggy version2.1.0 Laravel version11.1.1 DescriptionHi, When I use the In my import '../css/app.css'
import Layout from '@/layouts/default.vue'
import { createInertiaApp } from '@inertiajs/vue3'
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'
import { ZiggyVue } from 'ziggy-js'
import { Ziggy } from '@/routes/routes.js'
import { createApp, h } from 'vue'
createInertiaApp({
progress: {
color: '#4B5563',
},
resolve: (name) =>
resolvePageComponent(`./pages/${name}.vue`, import.meta.glob('./pages/**/*.vue')).then(
(module) => {
const page = module.default
page.layout = page.layout || Layout
return page
},
),
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue, Ziggy)
.mount(el)
},
}) In my const navigationItems = ref([
{ name: 'News', href: '/news', route: 'news.*', current: false },
{ name: 'Reviews', href: '/reviews', route: 'reviews', current: false },
{ name: 'Guides', href: '/guides', route: 'guides', current: false },
{ name: 'Schedule', href: '/schedule', route: 'schedule', current: false },
{ name: 'About', href: '/about', route: 'about', current: false },
])
onBeforeMount(() => {
router.on('navigate', () => {
const currentActive = navigationItems.value.findIndex((item) => item.current === true)
const newActive = navigationItems.value.findIndex((item) => {
if (item.route === null) {
return false
}
return route().current(item.route)
})
if (newActive === -1 && currentActive !== -1) {
navigationItems.value[currentActive].current = false
}
if (newActive > -1 && currentActive !== newActive) {
if (currentActive !== -1) {
navigationItems.value[currentActive].current = false
}
navigationItems.value[newActive].current = true
}
})
}) Ziggy call and contextUncaught TypeError: Cannot convert undefined or null to object
at Function.entries (<anonymous>)
at n.u (ziggy-js.js?v=3272b7cd:743:29)
at n.current (ziggy-js.js?v=3272b7cd:751:65)
at NavBar.vue:21:28
at Proxy.findIndex (<anonymous>)
at NavBar.vue:16:49
at HTMLDocument.n (@inertiajs_vue3.js?v=3272b7cd:4157:16)
at f (@inertiajs_vue3.js?v=3272b7cd:3878:19)
at S (@inertiajs_vue3.js?v=3272b7cd:3885:16)
at @inertiajs_vue3.js?v=3272b7cd:3959:96 Ziggy configuration{
url: 'http://localhost:8000',
port: 8000,
defaults: {},
routes: {
'filament.exports.download': {
uri: 'filament/exports/{export}/download',
methods: ['GET', 'HEAD'],
parameters: ['export'],
bindings: { export: 'id' },
},
'filament.imports.failed-rows.download': {
uri: 'filament/imports/{import}/failed-rows/download',
methods: ['GET', 'HEAD'],
parameters: ['import'],
bindings: { import: 'id' },
},
'filament.admin.auth.login': {
uri: 'admin/login',
methods: ['GET', 'HEAD'],
domain: 'admin.senshudo.tv',
},
'filament.admin.auth.logout': {
uri: 'admin/logout',
methods: ['POST'],
domain: 'admin.senshudo.tv',
},
'filament.admin.pages.dashboard': {
uri: 'admin',
methods: ['GET', 'HEAD'],
domain: 'admin.senshudo.tv',
},
'sanctum.csrf-cookie': { uri: 'sanctum/csrf-cookie', methods: ['GET', 'HEAD'] },
'livewire.update': { uri: 'livewire/update', methods: ['POST'] },
'livewire.upload-file': { uri: 'livewire/upload-file', methods: ['POST'] },
'livewire.preview-file': {
uri: 'livewire/preview-file/{filename}',
methods: ['GET', 'HEAD'],
parameters: ['filename'],
},
'ignition.healthCheck': { uri: '_ignition/health-check', methods: ['GET', 'HEAD'] },
'ignition.executeSolution': { uri: '_ignition/execute-solution', methods: ['POST'] },
'ignition.updateConfig': { uri: '_ignition/update-config', methods: ['POST'] },
'articles.index': { uri: 'api/articles', methods: ['GET', 'HEAD'] },
'articles.homepage': { uri: 'api/articles/homepage', methods: ['GET', 'HEAD'] },
'articles.featured': { uri: 'api/articles/featured', methods: ['GET', 'HEAD'] },
'articles.show': {
uri: 'api/articles/{article}',
methods: ['GET', 'HEAD'],
parameters: ['article'],
bindings: { article: 'slug' },
},
'categories.index': { uri: 'api/categories', methods: ['GET', 'HEAD'] },
'categories.show': {
uri: 'api/categories/{category}',
methods: ['GET', 'HEAD'],
parameters: ['category'],
bindings: { category: 'id' },
},
search: { uri: 'api/search', methods: ['GET', 'HEAD'] },
home: { uri: '/', methods: ['GET', 'HEAD'] },
'news.index': { uri: 'news', methods: ['GET', 'HEAD'] },
'news.show': {
uri: 'news/{article}',
methods: ['GET', 'HEAD'],
parameters: ['article'],
bindings: { article: 'slug' },
},
},
} Route definitionRoute::get('/', HomepageController::class)->name('home');
Route::prefix('news')->name('news.')->group(function () {
Route::get('/', [NewsController::class, 'index'])->name('index');
Route::get('/{article}', [NewsController::class, 'show'])->name('show');
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Your setup looks fine, it's weird that setting Are you using SSR? Does moving your |
Beta Was this translation helpful? Give feedback.
-
I came across the same problem when using route ziggy 1.8 in SSR laravel 10 + inertia outside of vue component, after 2 days of searching for a solution bakerkretzmar's post led me to the following solution. To use ziggy outside of vue, you need to create a Route instance and pass the path configuration to it. You can make a global default configuration by setting up the global object In ssr.js createServer(
(page) =>
createInertiaApp({
page,
render: renderToString,
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`../Pages/${name}.vue`, import.meta.glob('../Pages/**/*.vue')),
setup({ App, props, plugin }) {
globalThis.Ziggy = {
...page.props.ziggy,
location: new URL(page.props.ziggy.location),
};
return createSSRApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue, {
...page.props.ziggy,
location: new URL(page.props.ziggy.location),
})
.provide(ID_INJECTION_KEY, {
prefix: 1024,
current: 0,
});
},
})
); configure global settings by globalThis.Ziggy = {
...page.props.ziggy,
location: new URL(page.props.ziggy.location),
}; and then you can use ziggy as follows in some js file (example): import route from 'ziggy-js';
function getParams(page)
{
const params = route().params;
params._query = { page: page };
return params;
}
export function url(page)
{
return route(route().current(), getParams(page));
} |
Beta Was this translation helpful? Give feedback.
I came across the same problem when using route ziggy 1.8 in SSR laravel 10 + inertia outside of vue component, after 2 days of searching for a solution bakerkretzmar's post led me to the following solution. To use ziggy outside of vue, you need to create a Route instance and pass the path configuration to it. You can make a global default configuration by setting up the global object
globalThis.Ziggy
, which will then be used as the default. I got the following solution:In ssr.js