Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use router-link with ui-nav-item #17

Closed
leibale opened this issue Sep 27, 2020 · 8 comments
Closed

How to use router-link with ui-nav-item #17

leibale opened this issue Sep 27, 2020 · 8 comments

Comments

@leibale
Copy link

leibale commented Sep 27, 2020

Hi,

Is it possible to use router-link as a ui-nav-item in ui-drawer?

@elf-mouse
Copy link
Member

elf-mouse commented Sep 27, 2020

<ui-nav>
  <template v-for="(item, index) in menu" :key="index">
    <router-link v-slot="{ href, isActive }" :to="{ name: item.url }">
      <ui-nav-item :href="href" :active="isActive"></ui-nav-item>
    </router-link>
  </template>
</ui-nav>

@leibale
Copy link
Author

leibale commented Sep 27, 2020

The rendered HTML for

<router-link v-slot="{ href, isActive }" to="/">
  <ui-nav-item :href="href" :active="isActive"></ui-nav-item>
</router-link>

is

<a href="/"> <!-- router-link -->
  <a href="/" class="mdc-list-item" tabindex="-1"> <!-- ui-nav-item -->
    <span class="mdc-list-item__ripple"></span>
    Cart
  </a>
</a>

I want to have one a element per ui-nav-item..

@elf-mouse
Copy link
Member

  • <router-link v-slot> for Vue 2.0
  • <router-link custom v-slot> for Vue 3.0

try again!

@leibale
Copy link
Author

leibale commented Sep 29, 2020

Works like a charm, thanks

@leibale leibale closed this as completed Sep 29, 2020
@leibale leibale reopened this Sep 29, 2020
@leibale
Copy link
Author

leibale commented Sep 29, 2020

It is "working", but clicking the link makes the whole page refresh (probably cause ui-nav-item uses a element directly instead of using router-link)

@leibale leibale closed this as completed Sep 29, 2020
@leibale leibale reopened this Sep 29, 2020
@elf-mouse
Copy link
Member

Indeed! <ui-nav-item> was originally intended to define a hyperlink (like <a> or <router-link>)

  • If you want to prevent default event, you need to use @click.prevent for <a>/<ui-nav-item>/<router-link>
  • If you want to render non-hyperlink list, you can use <ui-list> + <ui-item @click>

@leibale
Copy link
Author

leibale commented Oct 15, 2020

Hi @elf-mouse, I'm trying to get the drawer to close when a link inside is clicked, but have no luck... :(
Can you please give me an example of a drawer with a router-link inside that closes when the link is clicked?

Thanks

@elf-mouse
Copy link
Member

<template>
  <!-- Drawer -->
  <ui-drawer v-model="open" type="modal">
    <ui-drawer-header>
      <ui-drawer-title>Test</ui-drawer-title>
    </ui-drawer-header>
    <ui-drawer-content>
      <ui-nav>
        <template v-for="(item, index) in links" :key="index">
          <router-link
            v-slot="{ href, isActive }"
            custom
            :to="{ name: item.url }"
          >
            <ui-nav-item
              :href="href"
              :active="isActive"
              @click.prevent="onLink(item)"
              >{{ item.name }}</ui-nav-item
            >
          </router-link>
        </template>
      </ui-nav>
    </ui-drawer-content>
  </ui-drawer>
  <ui-drawer-backdrop></ui-drawer-backdrop>
  <!-- Content -->
  <div class="demo-content">
    <!-- App bar -->
    <ui-top-app-bar
      fixed
      class="demo-app-bar"
      content-selector=".demo-app-content"
      @nav="balmUI.onOpen('open')"
    >
      Test
    </ui-top-app-bar>
    <!-- App content -->
    <div class="demo-app-content">
      <p v-for="i in 24" :key="i">Main Content {{ i }}</p>
    </div>
  </div>
</template>
import { useEvent } from 'balm-ui';

const links = [
  {
    name: 'Link1',
    url: 'test.main'
  },
  {
    name: 'Link2',
    url: 'test.sub'
  }
];

export default {
  data() {
    return {
      balmUI: useEvent(),
      links,
      open: false
    };
  },
  methods: {
    onLink(item) {
      this.open = false; // close drawer

      // this.$router.push({ name: item.url }); // redirect
    }
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants