Vuetify 3 – 导航栏组件占满整个视口高度

huangapple go评论63阅读模式
英文:

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:

Vuetify 3 – 导航栏组件占满整个视口高度

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:

Vuetify 3 – 导航栏组件占满整个视口高度

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:

Vuetify 3 – 导航栏组件占满整个视口高度

Here is my App.vue:

    &lt;template&gt;
  &lt;NavBar /&gt;
  &lt;HeroSection /&gt;
&lt;/template&gt;

&lt;script lang=&quot;ts&quot;&gt;
import { Options, Vue } from &quot;vue-class-component&quot;;
import NavBar from &quot;./components/NavBar.vue&quot;;
import HeroSection from &quot;./components/HeroSection.vue&quot;;

@Options({
  components: {
    NavBar,
    HeroSection,
  },
})
export default class App extends Vue {}
&lt;/script&gt;

&lt;style lang=&quot;scss&quot;&gt;&lt;/style&gt;

Here is my NavBar.vue:

&lt;!-- eslint-disable vue/valid-v-on --&gt;
&lt;template&gt;
  &lt;v-app :full-height=&quot;false&quot; class=&quot;app-bar-container&quot;&gt;
    &lt;v-app-bar :elevation=&quot;2&quot;&gt;
      &lt;!-- eslint-disable-next-line vue/valid-v-on --&gt;
      &lt;v-app-bar-nav-icon @click=&quot;drawer = !drawer&quot; id=&quot;hamburger-icon&quot;&gt;
      &lt;/v-app-bar-nav-icon&gt;
      &lt;v-app-bar-title&gt;The Candidate Consultants&lt;/v-app-bar-title&gt;
    &lt;/v-app-bar&gt;
    &lt;v-navigation-drawer v-model=&quot;drawer&quot; app&gt;
      &lt;v-list&gt;
        &lt;v-list-item v-for=&quot;item in menuItems&quot; :key=&quot;item.title&quot; @click=&quot;&quot;&gt;
          &lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
        &lt;/v-list-item&gt;
      &lt;/v-list&gt;
    &lt;/v-navigation-drawer&gt;
  &lt;/v-app&gt;
&lt;/template&gt;

&lt;script lang=&quot;ts&quot;&gt;
import { defineComponent, ref } from &quot;vue&quot;;
import {
  VAppBar,
  VNavigationDrawer,
  VList,
  VListItem,
  VListItemTitle,
} from &quot;vuetify/components&quot;;

export default defineComponent({
  components: {
    VAppBar,
    VNavigationDrawer,
    VList,
    VListItem,
    VListItemTitle,
  },
  setup() {
    const drawer = ref(false);
    const menuItems = [
      { title: &quot;Home&quot; },
      { title: &quot;About&quot; },
      { title: &quot;Individuals&quot; },
      { title: &quot;Corporate&quot; },
      { title: &quot;Blog&quot; },
      { title: &quot;Contact&quot; },
    ];

    return {
      drawer,
      menuItems,
    };
  },
});
&lt;/script&gt;

&lt;style scoped lang=&quot;scss&quot;&gt;
.app-bar-container {
  height: auto;
}
&lt;/style&gt;

And my HeroSection component:

&lt;template&gt;
  &lt;div class=&quot;hero-section&quot;&gt;&lt;/div&gt;
&lt;/template&gt;
&lt;script lang=&quot;ts&quot;&gt;
import { Options, Vue } from &quot;vue-class-component&quot;;

@Options({
  name: &quot;HeroSection&quot;,
})
export default class HeroSection extends Vue {}
&lt;/script&gt;
&lt;style lang=&quot;scss&quot;&gt;
.hero-section {
  background-image: url(&quot;../assets/landing_hero.png&quot;);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  height: 100vh;
  width: 100%;
}
&lt;/style&gt;

As you can see, I tried adding :full-height=&quot;false&quot; 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:

  1. That v-app-bar must be inside v-app.

  2. 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:

&lt;v-app&gt;
  &lt;NavBar /&gt;
  &lt;v-main&gt;
    &lt;HeroSection /&gt;
  &lt;/v-main&gt;
&lt;/v-app&gt;

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;
    
  }

huangapple
  • 本文由 发表于 2023年5月6日 20:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定