Re: NavigationMenu throw error TypeError: Cannot read properties of undefined (reading 'items')

Solved

NavigationMenu throw error TypeError: Cannot read properties of undefined (reading 'items')

MEHTEC
Shopify Partner
5 1 1

I create Admin App on Angular 19. SPA with routing.

I want to add navigation menu in admin panel on merchant store.

For example menu with two items

Dashboard

Settings

in npm I install @Shopify/app-bridge

1. create app-bridge

   this.app = createApp({ apiKey: 'SHOPIFY_API_KEY', host: host });
2. create NavigationMenu with items

   

const dashboardLink = AppLink.create(this.app, {
label: 'Dashboard',
destination: '/',
});

const settingsLink = AppLink.create(this.app, {
label: 'Settings',
destination: '/settings',
});

this.menu = NavigationMenu.create(this.app, {
items: [dashboardLink, settingsLink],
})

after some timeout add menu to App

setTimeout(() => { this.app.dispatch(this.menu); }, 500);

but I catch exeption

TypeError: Cannot read properties of undefined (reading 'items')
    at getPayloadSchema (menu.js:48:23)
    at Object.validateAction (menu.js:72:85)
    at dispatchPayloadValidator (validator.js:51:29)
    at dispatchValidator (validator.js:90:23)
    at Object.<anonymous> (validator.js:113:23)
    at handler (Hooks.js:89:38)
    at Object.<anonymous> (Client.js:54:12)
    at Object.handler (Hooks.js:89:38)
    at Hooks2.run (Hooks.js:93:20)
    at Object.dispatch (Client.js:142:26)

despite this the menu appears in the admin panel

MEHTEC_0-1739381399286.png

 

And secont question

when I navigating by menu item my browser URL is changing according 'destination'. But my SPA routing in Angular does not load the corresponding component. Internal iframe not "feel" of URL changing. How to create menu that will change routing is SPA but not in the browser URL

 

Accepted Solution (1)

MEHTEC
Shopify Partner
5 1 1

This is an accepted solution.

The problem was

setTimeout(() => { this.app.dispatch(this.menu); }, 500);

There no need to dispatch menu to add it on admin panel.

 

creating a NavigationMenu is enough

this.menu = NavigationMenu.create(this.app, {
items: [dashboardLink, settingsLink],
})

But the second problem remains
The NavigationMenu does not switch routes inside the SPA application!

View solution in original post

Reply 1 (1)

MEHTEC
Shopify Partner
5 1 1

This is an accepted solution.

The problem was

setTimeout(() => { this.app.dispatch(this.menu); }, 500);

There no need to dispatch menu to add it on admin panel.

 

creating a NavigationMenu is enough

this.menu = NavigationMenu.create(this.app, {
items: [dashboardLink, settingsLink],
})

But the second problem remains
The NavigationMenu does not switch routes inside the SPA application!