如何在vue.js中的重新订票上打开多层B崩溃菜单?

发布于 2025-02-10 23:50:02 字数 3244 浏览 1 评论 0原文

我正在努力寻找最佳的方法,以使我的多层菜单在Refresh上打开。

基本上,如果有人钻到我菜单中的第三层并单击重新修订,那么我希望菜单打开回到他们在第三层中导航到的地方,而不必重新打开菜单。

请参阅下面的代码:(使用Vue-Bootstrap和vue.js)

如您所见,我正在从数据中重复每一层。

        <div v-for="(main, index) in mainMenu" :key="index">
          <template v-if="main.title === 'News'">
            <b-link @click="closeAll" v-if="main.type === 'post_type' " :to="'/' + main.slug" v-b-toggle="'collapse'"  class="m-1 nobutn" v-html="main.title"></b-link>
          </template>
          <template v-else>
            <b-link v-if="main.type === 'post_type' " :to="'/' + main.slug" v-b-toggle="main.post_name" class="m-1 nobutn" v-html="main.title"></b-link>
            <b-link v-else :to="'/' + main.url" v-b-toggle="main.post_name" class="m-1 nobutn" v-html="main.title"></b-link>
          </template>
          <b-collapse v-model="main.show" v-for="(sub, index) in main.child_items" :key="index" v-bind:id="sub.menu_item_parent" accordion="my-accordion" role="tabpanel">
            <template v-if="sub.type === 'post_type'">
              <b-link class="secondLayer" :to="'/' + main.slug + '/' + sub.slug" :class="sub.classes">
                <b-card v-b-toggle="sub.post_name" class="subTitle" v-html="sub.title"></b-card>
              </b-link>
            </template>
            <template v-else>
              <b-link :href="sub.url" target="_blank" :class="sub.classes">
                <b-card v-b-toggle="sub.post_name" class="subTitle" v-html="sub.title"></b-card>
              </b-link>               
            </template>
            <b-collapse v-for="(subchild, index) in sub.child_items" :key="index" v-bind:id="subchild.menu_item_parent" accordion="my-accordiontwo" role="tabpanel">
              <template v-if="subchild.title === 'Members Portal' ">
                <b-link class="thirdLayer" :href="subchild.url" target="_blank" :class="subchild.classes">
                  <b-card v-b-toggle="subchild.post_name" class="subchildTitle" v-html="subchild.title"></b-card>
                </b-link>
              </template>
              <template v-else>
                <b-link class="thirdLayer" :to="'/' + main.slug + '/' + sub.slug + '/' + subchild.slug" :class="subchild.classes">
                  <b-card v-b-toggle="subchild.post_name" class="subchildTitle" v-html="subchild.title"></b-card>
                </b-link>
              </template>
              <b-collapse v-for="(fourthsubchild, index) in subchild.child_items" :key="index" :id="fourthsubchild.menu_item_parent">
                <b-link class="fouthLayer" :to="'/' + main.slug + '/' + sub.slug + '/' + subchild.slug + '/' + fourthsubchild.slug" :class="fourthsubchild.classes">
                  <b-card class="fourthchildTitle" v-html="fourthsubchild.title"></b-card>
                </b-link>
              </b-collapse>
            </b-collapse>
          </b-collapse>
        </div>

提前致谢。

I'm struggling to find the best way to keep my multi-layer menu open on re-fresh.

Basically if someone drills down to the third layer in my menu and clicks re-fresh, then I want the menu to open back up to where they navigated to in the third layer without having to re-open the menu.

Please see my code below: (Using Vue-Bootstrap and Vue.js)

As you can see, I'm repeating over each layer I get from my data.

        <div v-for="(main, index) in mainMenu" :key="index">
          <template v-if="main.title === 'News'">
            <b-link @click="closeAll" v-if="main.type === 'post_type' " :to="'/' + main.slug" v-b-toggle="'collapse'"  class="m-1 nobutn" v-html="main.title"></b-link>
          </template>
          <template v-else>
            <b-link v-if="main.type === 'post_type' " :to="'/' + main.slug" v-b-toggle="main.post_name" class="m-1 nobutn" v-html="main.title"></b-link>
            <b-link v-else :to="'/' + main.url" v-b-toggle="main.post_name" class="m-1 nobutn" v-html="main.title"></b-link>
          </template>
          <b-collapse v-model="main.show" v-for="(sub, index) in main.child_items" :key="index" v-bind:id="sub.menu_item_parent" accordion="my-accordion" role="tabpanel">
            <template v-if="sub.type === 'post_type'">
              <b-link class="secondLayer" :to="'/' + main.slug + '/' + sub.slug" :class="sub.classes">
                <b-card v-b-toggle="sub.post_name" class="subTitle" v-html="sub.title"></b-card>
              </b-link>
            </template>
            <template v-else>
              <b-link :href="sub.url" target="_blank" :class="sub.classes">
                <b-card v-b-toggle="sub.post_name" class="subTitle" v-html="sub.title"></b-card>
              </b-link>               
            </template>
            <b-collapse v-for="(subchild, index) in sub.child_items" :key="index" v-bind:id="subchild.menu_item_parent" accordion="my-accordiontwo" role="tabpanel">
              <template v-if="subchild.title === 'Members Portal' ">
                <b-link class="thirdLayer" :href="subchild.url" target="_blank" :class="subchild.classes">
                  <b-card v-b-toggle="subchild.post_name" class="subchildTitle" v-html="subchild.title"></b-card>
                </b-link>
              </template>
              <template v-else>
                <b-link class="thirdLayer" :to="'/' + main.slug + '/' + sub.slug + '/' + subchild.slug" :class="subchild.classes">
                  <b-card v-b-toggle="subchild.post_name" class="subchildTitle" v-html="subchild.title"></b-card>
                </b-link>
              </template>
              <b-collapse v-for="(fourthsubchild, index) in subchild.child_items" :key="index" :id="fourthsubchild.menu_item_parent">
                <b-link class="fouthLayer" :to="'/' + main.slug + '/' + sub.slug + '/' + subchild.slug + '/' + fourthsubchild.slug" :class="fourthsubchild.classes">
                  <b-card class="fourthchildTitle" v-html="fourthsubchild.title"></b-card>
                </b-link>
              </b-collapse>
            </b-collapse>
          </b-collapse>
        </div>

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2025-02-17 23:50:02

一个很难的答案很难,因为我不知道您的设置,并且对Vue-Bootstrap并不熟悉。

它可以在组件设置函数中看起来像这样的东西:

onBeforeMount(() => {
  const thirdMenu = sessionStorage.getItem("thirdMenu");
  if (thirdMenu) {
    yourVariableWhatMenuIsOpen = thirdMenu; // open your third level menu as you want
  }
});

watch(
  () => yourVariableWhatMenuIsOpen,
  () => {
    yourVariableWhatMenuIsOpen
      ? sessionStorage.setItem("key", yourVariableWhatMenuIsOpen)
      : sessionStorage.removeItem("key");
  }
);

yourvariable whatmenuisopen您可能是您为菜单初始化的东西。
而不是 watcher 您可以使用a

希望,这会有所帮助。也许您必须深入研究Vue,但我保证,这很有趣。 :)

A detailled answer is hard, because I don't know your setup and am not familliar with vue-bootstrap.

It could look something like this in your components setup function:

onBeforeMount(() => {
  const thirdMenu = sessionStorage.getItem("thirdMenu");
  if (thirdMenu) {
    yourVariableWhatMenuIsOpen = thirdMenu; // open your third level menu as you want
  }
});

watch(
  () => yourVariableWhatMenuIsOpen,
  () => {
    yourVariableWhatMenuIsOpen
      ? sessionStorage.setItem("key", yourVariableWhatMenuIsOpen)
      : sessionStorage.removeItem("key");
  }
);

yourVariableWhatMenuIsOpen is something you probably have initialized for your menu.
Instead of a watcher you could use a reactive state.

Hope, this helps. Maybe you have to dig a bit deeper into Vue, but it will be all fun, I promise. :)

梦屿孤独相伴 2025-02-17 23:50:02

页面刷新设置您的整个页面。
您可以在 SessionStorage
在打开菜单和安装组件之前设置值,请尝试从会话存储中获取键值。如果可用,请设置菜单重新打开。

编辑:对不起,对于动态的第三层,您应该保存字符串或ID,而不是布尔值。 )

A page refresh sets back your whole page.
You could save a boolean e. g. menuIsOpen in your sessionStorage.
Set the value when the menu is opened and before your component is mounted, try to get the key-value from the session storage. If it's available, set the menu to open again.

Edit: Sorry, for a dynamic third layer you should save a string or id rather than a boolean. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文