僅當在vuejs中按一下父項時才顯示子項
P粉574695215
P粉574695215 2024-02-25 19:20:16
0
1
369

我正在嘗試建立一個導覽側邊欄。會有主項目,也會有子項目。

我嘗試僅在單擊父項目時顯示子項目,並且當我單擊子項目時,我希望活動子項目具有不同的顏色。我如何實現這個目標?

這是我到目前為止所嘗試過的。

<template>
  <div>
    <nav>
      <ul>
        <li v-for="(link, index) in navLinks" :key="index">
          <router-link :to="link.path" class-active="active">
            <span class="items">
              {{ link.text }}
            </span>
          </router-link>
          <div v-if="link.sublinks && link.sublinks.length > 0"> //I want it to show only when the parent item is clicked
            <li v-for="(link, index) in belowLinks" :key="index">
              <router-link :to="link.path" class-active="sub-active"> //trying to add the sub-active class but it's not working
                <span class="sub-items">
                  {{ link.text }}
                </span>
              </router-link>
            </li>
          </div>
        </li>
      </ul>
    </nav>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navLinks: [
        {
          text: 'Contact',
          path: '/contact',
          sublinks: [
            {
              text: 'Email',
              path: '/email',
            },
          ],
        },
        {
          text: 'About',
          path: '/about',
        },
      ],
      belowLinks: [
        {
          text: 'Blog',
          path: '/blog',
        },
        {
          text: 'Portfolio',
          path: '/portfolio',
        },
      ],
    };
  },
};
</script>
<style scoped >
nav {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #040521;
  justify-content: space-between;
}
ul {
  display: flex;
  align-items: center;
  margin-block-start: 0;
  margin-block-end: 0;
  padding-inline-start: 0;
  flex-direction: column;
}
a {
  text-decoration: none;
  display: flex;
  align-items: center;
  color: white;
}
a:hover {
  color: white;
}
li {
  list-style-type: none;
  padding: 10px 0px;
  width: 100%;
}
.page-link .active,
.router-link-active {
  background-color: green;
  color: white !important;
  border-color: inherit !important;
}
.sub-active {
  background-color: yellow !important;
  color: white !important;
  border-color: inherit !important;
}
.items {
  padding: 10px 20px;
}
.sub-items {
  padding: 10px 0px 10px 40px;
}
</style>

P粉574695215
P粉574695215

全部回覆(1)
P粉132730839

嘗試像下面的程式碼片段(您在巢狀連結中錯過了另一個 ul,然後只需使用清單索引切換顯示/隱藏導航):

new Vue({
  el: "#demo",
  data() {
    return {
      navLinks: [
          {text: 'Contact', path: '/contact', sublinks: [{ text: 'Email', path: '/email',},],
          },
          {text: 'About', path: '/about',},
        ],
      belowLinks: [
        {text: 'Blog', path: '/blog',},
        {text: 'Portfolio', path: '/portfolio',},
      ],
      show: null,
      active: null
    }
  },
  methods: {
    toggleNav(i) {
      this.active = null
      this.show === i ? this.show = null : this.show = i
    },
    setActive(i) {
      this.active === i ? this.active = null : this.active = i
    }
  }
})
nav {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #040521;
  justify-content: space-between;
}
ul {
  display: flex;
  align-items: center;
  margin-block-start: 0;
  margin-block-end: 0;
  padding-inline-start: 0;
  flex-direction: column;
}
a {
  text-decoration: none;
  display: flex;
  align-items: center;
  color: white;
}
a:hover {
  color: white;
}
li {
  list-style-type: none;
  padding: 10px 0px;
  width: 100%;
}
.page-link .active,
.router-link-active {
  background-color: green;
  color: white !important;
  border-color: inherit !important;
}
.sub-active {
  background-color: yellow !important;
  color: white !important;
  border-color: inherit !important;
}
.items {
  padding: 10px 20px;
}
.sub-items {
  padding: 10px 0px 10px 40px;
}

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!