18
votes

I have a single-page app that i've created using vue, and the nav links are all done using router-link tags. There are a couple of items in the nav that my boss wants to have in the nav but disabled so that people can get a glimpse of some features that will be coming soon. However I can't figure out how to completely disable a router-link!

preventDefault does nothing, @click.native.prevent="stopClick()" does nothing (i tried sending it to a function to see if that would prevent the click but it just calls the function and routes anyway despite the prevent), adding a disabled class and setting a css rule of pointer-events: none; does nothing. I'm not sure what else to try, is the only way around this to make the disabled links normal text and not router-links?

11

11 Answers

13
votes

There is still no native solution today. But there is an open PR for this on the vue-router repo : https://github.com/vuejs/vue-router/pull/2098.

A workaround is to use :

<router-link 
  :disabled="!whateverActivatesThisLink" 
  :event="whateverActivatesThisLink ? 'click' : ''"
  to="/link"
>
  /link
</router-link>
9
votes

I don't think there's a suitable solution for this problem since router links do not have the disabled attribute, but one trick would be using tag="button" in order to add the required attribute as follows:

<router-link 
     to="/link"
     tag="button"
     :disabled="true"
>
  Link
</router-link>
6
votes

There is nothing built in, and probably won't ever be. That said, what worked great for me is to use CSS.

<router-link to="/my-route" :class="{ disabled: someBoolean }" />
.disabled {
    opacity: 0.5;
    pointer-events: none;
}

The opacity makes it look disabled, and the pointer-events: none; makes it so you don't need to also handle :hover styles, or set the cursor style.

6
votes

You can use

<router-link 
  :is="isDisabled ? 'span' : 'router-link'"
  to="/link"
>
  /link
</router-link>
3
votes

Method 1: Prevent the click event

The trick is to handle an event on a capture phase and stop it from propagating up top.

<router-link 
  to="/path"
  @click.native.capture.stop
>
  Go to page
</router-link>

Or imperatively:

<router-link 
  to="/path"
  @click.native.capture="handleClick"
>
  Go to page
</router-link>
function handleClick(event) {
  if (passesSomeCheck) event.stopPropagation();
}

This might be very useful if you want to get the resolved path from Vue Router to force a page load without SPA navigation.

function handleClick(event) {
  if (loadWithoutSpa) {
    event.stopPropagation();
    window.location.href = event.currentTarget.href;
  };
}

Method 2: Change default event to an empty string

<router-link 
  to="/path"
  event
>
  Go to page
</router-link>

Method 3: Use an <a> tag

<a :href="$router.resolve(route).href">
  Go to page
</a>

Where route can be exactly the same thing you pass to a to prop on router-link.

2
votes

Just set to="" then the link doesn't go anywhere.

1
votes

You can set route guard per-route

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      beforeEnter: (to, from, next) => {
        //here you can set if condition 
        if (conditionTrue) {
          //redirect to other route
          next({path: '/bar'});
        } else {
          next();
        }
      }          
    }
  ]
})

Or you can set globally

routes: [
  {path: '/foo', component: Foo, meta:{conditionalRoute: true}}
]; 

router.beforeEach((to, from, next) => { 
    if (to.matched.some(record => record.meta.conditionalRoute)) { 
        // this route requires condition/permission to be accessed
        if (!checkCondition ) { 
            //check condition is false
            next({ path: '/'});
        } else { 
            //check condition is true
            next();
        } 
    } else { 
        next(); // Don't forget next
    } 
})

For more info: https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards

0
votes

To prevent a click you can directly access to event property of the router-link element like this (and you can use native click to do something else) :

<router-link 
:event="clickable ? 'click' : ''" 
@click.native="!clickable ? doOtherThing : ''" > Link </router-link>
0
votes

Update for Router v4:

Using a boolean variable status to determine if the link is active or not and passing the link (ctalink) as a variable as well.

Stumbled in here coming from nuxt-link implementation that broke with the update, so from experience this works likewise.

    <router-link
      v-slot="{ navigate }"
      :to="ctalink"
      custom
    >
      <div @click="status ? navigate(ctalink) : null">
        <div :class="status ? 'text-green' : 'text-gray'"> 
          Click me when active 
        </div>
      </div>
    </router-link>

Source: https://next.router.vuejs.org/api/#router-link-s-v-slot

0
votes

NuxtLink (Vue Router v4)

To me what worked like a charm was the code below. This is a real code that I'm using in my application with nuxt and tailwind.

              <nuxt-link
                v-slot="{ navigate }"
                :to="`lesson/${lesson.lesson_id}`"
                append
                custom
              >
                <button
                  class="focus:outline-none"
                  :class="{
                    'text-gray-500 cursor-default': !lesson.released,
                  }"
                  :disabled="!lesson.released"
                  @click="navigate"
                >
                  {{ lesson.title }}
                </button>
              </nuxt-link>
0
votes

You can try:

  <router-link
   :event="whateverActivatesThisLink ? 'click' : ''"
  >
  Go to page
  </router-link>

Or write a cleaner code with computed:

  <router-link
  :event="handleWhatEverEvent"
  >
  Go to page
  </router-link>

computed: {
  handleWhatEverEvent() {
    return this.whateverActivatesThisLink ? 'click' : '';
  }
}