英文:
Vuetify 3 - NavBar component taking up full viewport height
问题
以下是翻译好的部分:
我的 NavBar 组件占据了整个视口高度,即使其子元素没有占满:
我这里不需要这么多的空白。
子元素 v-app-bar 在 v-app 内正确显示,而且不会占用额外的空间:
英雄部分也占用了正确的空间,但只有在滚动到屏幕底部以下才能看到:
这是我的 App.vue:
<template>
<NavBar />
<HeroSection />
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import NavBar from "./components/NavBar.vue";
import HeroSection from "./components/HeroSection.vue";
@Options({
components: {
NavBar,
HeroSection,
},
})
export default class App extends Vue {}
</script>
<style lang="scss"></style>
这是我的 NavBar.vue:
<template>
<v-app :full-height="false" class="app-bar-container">
<v-app-bar :elevation="2">
<v-app-bar-nav-icon @click="drawer = !drawer" id="hamburger-icon"></v-app-bar-nav-icon>
<v-app-bar-title>The Candidate Consultants</v-app-bar-title>
</v-app-bar>
<v-navigation-drawer v-model="drawer" app>
<v-list>
<v-list-item v-for="item in menuItems" :key="item.title" @click="">
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-app>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";
import {
VAppBar,
VNavigationDrawer,
VList,
VListItem,
VListItemTitle,
} from "vuetify/components";
export default defineComponent({
components: {
VAppBar,
VNavigationDrawer,
VList,
VListItem,
VListItemTitle,
},
setup() {
const drawer = ref(false);
const menuItems = [
{ title: "Home" },
{ title: "About" },
{ title: "Individuals" },
{ title: "Corporate" },
{ title: "Blog" },
{ title: "Contact" },
];
return {
drawer,
menuItems,
};
},
});
</script>
<style scoped lang="scss">
.app-bar-container {
height: auto;
}
</style>
我的 HeroSection 组件:
<template>
<div class="hero-section"></div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
@Options({
name: "HeroSection",
})
export default class HeroSection extends Vue {}
</script>
<style lang="scss">
.hero-section {
background-image: url("../assets/landing_hero.png");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
height: 100vh;
width: 100%;
}
</style>
希望这可以帮助你理解如何解决问题并正确布局这些组件,以便它们只占用正确的空间。如果有其他问题,请随时提出。
英文:
My NavBar component takes up the full viewport height even though its child elements do not:
I don't need all the white space here.
The child element v-app-bar inside v-app displays correctly and doesn't take up any extra space:
The hero section also takes up the right amount of space, but you can't see it until you scroll down past the bottom of the screen:
Here is my App.vue:
<template>
<NavBar />
<HeroSection />
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import NavBar from "./components/NavBar.vue";
import HeroSection from "./components/HeroSection.vue";
@Options({
components: {
NavBar,
HeroSection,
},
})
export default class App extends Vue {}
</script>
<style lang="scss"></style>
Here is my NavBar.vue:
<!-- eslint-disable vue/valid-v-on -->
<template>
<v-app :full-height="false" class="app-bar-container">
<v-app-bar :elevation="2">
<!-- eslint-disable-next-line vue/valid-v-on -->
<v-app-bar-nav-icon @click="drawer = !drawer" id="hamburger-icon">
</v-app-bar-nav-icon>
<v-app-bar-title>The Candidate Consultants</v-app-bar-title>
</v-app-bar>
<v-navigation-drawer v-model="drawer" app>
<v-list>
<v-list-item v-for="item in menuItems" :key="item.title" @click="">
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-navigation-drawer>
</v-app>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";
import {
VAppBar,
VNavigationDrawer,
VList,
VListItem,
VListItemTitle,
} from "vuetify/components";
export default defineComponent({
components: {
VAppBar,
VNavigationDrawer,
VList,
VListItem,
VListItemTitle,
},
setup() {
const drawer = ref(false);
const menuItems = [
{ title: "Home" },
{ title: "About" },
{ title: "Individuals" },
{ title: "Corporate" },
{ title: "Blog" },
{ title: "Contact" },
];
return {
drawer,
menuItems,
};
},
});
</script>
<style scoped lang="scss">
.app-bar-container {
height: auto;
}
</style>
And my HeroSection component:
<template>
<div class="hero-section"></div>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
@Options({
name: "HeroSection",
})
export default class HeroSection extends Vue {}
</script>
<style lang="scss">
.hero-section {
background-image: url("../assets/landing_hero.png");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
height: 100vh;
width: 100%;
}
</style>
As you can see, I tried adding :full-height="false"
to v-app as it says in the docs that full-height is true by default: https://vuetifyjs.com/en/api/v-app/
I also tried adding a class of .app-bar-container then giving it a height of auto to try and remove any default vuetify styles that may have been causing this.
My assumptions are:
-
That v-app-bar must be inside v-app.
-
That this layout is correct in App.vue, with the components themselves as direct child elements of <template>.
<template>
<NavBar />
<HeroSection />
</template>
Please help me to understand how to resolve this issue and how to lay these components out so they only take up the right amount of space moving forward. Thanks in advance!
答案1
得分: 1
是的,v-app-bar
必须位于 v-app
内,因为它依赖于提供的主题。
Vuetify 旨在包含整个应用程序,所以预期的布局应该是:
<v-app>
<NavBar />
<v-main>
<HeroSection />
</v-main>
</v-app>
如果您不想使用 Vuetify 布局,我认为最好的选择是从应用程序包装器中删除 min-height
:
.v-application .v-application__wrap{
min-height: unset;
}
英文:
Yes, v-app-bar
must be inside v-app
, as it depends on provided themes.
Vuetify is meant to contain the whole application, so the intended layout would be:
<v-app>
<NavBar />
<v-main>
<HeroSection />
</v-main>
</v-app>
If you do not want to use the Vuetify layout, I think your best option is to remove the min-height
from the app wrapper:
.v-application .v-application__wrap{
min-height: unset;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论