Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Vuetify throws Could not find injected layout when a component mounts

Your error

Error: [Vuetify] Could not find injected layout

Fix it fast

Most common cause, 1 of 3

The test mounts the component without a layout wrapper

Mount a small wrapper whose template puts your component inside v-layout, register the component there, and pass the Vuetify instance as a global plugin.

Fix
import { mount } from '@vue/test-utils'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
import AppShell from '../AppShell.vue'

const vuetify = createVuetify({ components, directives })

const wrapper = mount(
  { template: '<v-layout><app-shell /></v-layout>' },
  { global: { components: { AppShell }, plugins: [vuetify] } },
)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

[Vuetify] Could not find injected layout is thrown when v-app-bar, v-navigation-drawer, v-main, v-bottom-navigation, v-system-bar or a footer with the app prop renders without a v-app or v-layout ancestor. In Vitest, mount the component inside a v-layout wrapper; in the app, keep those components below v-app. Applies to Vuetify 3.x and 4.3.

  • Applies to Vuetify 4.3.4, Vuetify 3.x, Vue 3.5.46, @vue/test-utils 2.6.0, Vitest 5.1.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run only the failing test, or open the browser console, and read the component names in the stack trace under the error. A layout part (VAppBar, VNavigationDrawer, VMain, VBottomNavigation, VSystemBar, VFooter) tells you which element lacks a layout parent; a call from your own script setup points at cause 3.

Run this first
npx vitest run src/components/__tests__/AppShell.spec.ts 2>&1 | grep -A12 "Could not find injected layout"

Then find the line you see in this list and open its fix:

  1. 1The test mounts the component without a layout wrapperThe app runs fine in the browser and only the unit test throws
  2. 2A layout component sits outside v-app in the real appThe browser console shows the error on page load, and the component is in App.vue above or beside v-app, or in a second Vue app created with createApp
  3. 3useLayout() is called in the component that renders v-appThe stack trace points at your own setup code calling useLayout, not at a Vuetify component

What does the error look like?

  • Error: [Vuetify] Could not find injected layout

Cause 1 of 3

The test mounts the component without a layout wrapper

You seeThe app runs fine in the browser and only the unit test throws

Check

In a unit test, Could not find injected layout appears because mount() renders your component as the root, so nothing above it provides the layout. Vuetify's layout parts read that value from a v-app or v-layout ancestor and throw when it is missing. Vuetify's own testing guide mounts components inside a v-layout for this reason.

Check
grep -rn "mount(" src --include=*.spec.ts | head

Fix

Mount a small wrapper whose template puts your component inside v-layout, register the component there, and pass the Vuetify instance as a global plugin.

Fix
import { mount } from '@vue/test-utils'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
import AppShell from '../AppShell.vue'

const vuetify = createVuetify({ components, directives })

const wrapper = mount(
  { template: '<v-layout><app-shell /></v-layout>' },
  { global: { components: { AppShell }, plugins: [vuetify] } },
)

Verify

The test passes and wrapper.html() contains the app bar or drawer markup.

Verify
npx vitest run src/components/__tests__/AppShell.spec.ts

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

A layout component sits outside v-app in the real app

You seeThe browser console shows the error on page load, and the component is in App.vue above or beside v-app, or in a second Vue app created with createApp

Check

Vue's provide and inject only pass values down to descendants, so a v-app-bar or v-main placed next to v-app, or rendered by a separate app instance, cannot see the layout. A v-footer counts only when it has the app prop, because Vuetify registers the footer as a layout item just in that case.

Check
grep -n "<v-app\|<v-layout\|<v-app-bar\|<v-main\|<v-footer" src/App.vue src/layouts/*.vue

Fix

Make v-app the outermost element of the root component and put every layout part inside it. For a footer that should not take part in the layout, drop the app prop.

Fix
<template>
  <v-app>
    <v-app-bar title="Shop" />
    <v-navigation-drawer />
    <v-main>
      <router-view />
    </v-main>
    <v-footer app>Footer</v-footer>
  </v-app>
</template>

Verify

The page loads without the error and v-main leaves room for the app bar.

Verify
npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

useLayout() is called in the component that renders v-app

You seeThe stack trace points at your own setup code calling useLayout, not at a Vuetify component

Check

When useLayout() itself throws Could not find injected layout, the call is usually made in the same component whose template holds v-app. That component is the provider's parent, not its child, so the layout does not exist for it yet. Vuetify's layout guide warns that the call must happen in a child component.

Check
grep -rn "useLayout()" src

Fix

Move the code that reads layout sizes into a component rendered inside v-app, and call useLayout() there.

Fix
<!-- LayoutInfo.vue, used inside <v-main> -->
<script setup>
import { useLayout } from 'vuetify'
const { mainRect } = useLayout()
</script>
<template><span>{{ mainRect }}</span></template>

Verify

The child component renders the layout values and the root no longer throws.

Verify
npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep one test helper that mounts components inside v-layout with the Vuetify plugin, and use it in every component test.
  • Put v-app only in App.vue or a layout file, and review any new createApp call for a missing v-app root.
  • Add the app prop to v-footer only when the footer should reserve space in the layout.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.