lucafj2j282j
lucafj2j282j

Reputation: 891

Hide vue router from a page

Is it possible to hide the vue-router from my login page? If so, how would I do that? On every page I have I see the menu, but on the Login page I don't want to see it.

Here is my code:

Login

 <template>
 <div>
     <h1>Login</h1>
        <form action="">
            <label>naam</label>
            <input type="text">
        </form>
    </div>
</template>

<script>

</script>


<style scoped>
    h1 {
        background-color: chartreuse;
    }
</style>

App.vue

<template>
  <div id="app">
    <div class="routing">

    </div>
    <router-link to="/">Login</router-link>
    <router-link to="/home">Home</router-link>
    <router-view v-if="$route = 'login'"></router-view>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  }
}
</script>

Main.js

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import App from './App.vue'
    import Login from './Login.vue'
    import Home from './Home.vue'

    Vue.use(VueRouter);

    const routes = [
        { path: '/', component: Login},
        { path: '/home', component: Home},
    ];

    const router = new VueRouter({
      routes,
        mode: 'history'
    });

    new Vue({
      el: '#app',
        router,
      render: h => h(App)
    });

Home.vue

<template>
    <div>
        <h1>Home</h1>
        <hr>
        <router-view></router-view>
    </div>
</template>

<script>

</script>


<style scoped>
h1 {
    background-color: aquamarine;
}
</style>

enter image description here

Upvotes: 4

Views: 17957

Answers (3)

dime2lo
dime2lo

Reputation: 836

I just had the same problem and found 2 approaches:

1. Using nested routes - http://router.vuejs.org/en/essentials/nested-routes.html

Basically you have to use your App.vue as kind of a placeholder, with all the elements that are shared between all the pages (in your case I believe that is none), and place <router-view></router-view> inside it. For you I believe that will be as simples as:

//App.vue

<template>
  <div id="app">
      <router-view></router-view>
  </div>
</template>

And you also will have to have another template that will hold your menu:

//Restricted.vue

<template>
  <div id="restricted">
    <div class="routing">

    </div>
    <router-link to="/">Login</router-link>
    <router-link to="/home">Home</router-link>
    <router-view></router-view>
  </div>
</template>

And in your router should have something like:

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import Login from './Login.vue'
import Home from './Home.vue'
import Restricted from '/.Restricted.vue';

Vue.use(VueRouter);

const routes = [
    { path: '/', component: Login},
    {
        path: '/restricted',
        component: Restricted,
        children: [
           { path: 'home', component: Home},
        ],
    },        
];

const router = new VueRouter({
  routes,
  mode: 'history'
});

new Vue({
  el: '#app',
    router,
  render: h => h(App)
});

This way your Login page will be rendered at / and other pages (with menu) at /restricted/{other_page}. Using this approach the menu will not be displayed nor rendered.

2. Using v-if on the components that you do not want to render.

In you App.vue use v-if="this.$route.path !== '/'" on the elements that you do not want to be rendered, in your case, the router-link's. You could encapsulate them and apply v-if="this.$route.path !== '/'" to the encapsulation element (div?)

//App.vue
<template>
  <div id="app">
    <div class="routing" v-if="this.$route.path !== '/'">
        <router-link to="/">Login</router-link>
        <router-link to="/home">Home</router-link>
    </div>

    <router-view></router-view>
  </div>
</template>

Regards.

Upvotes: 14

forgere
forgere

Reputation: 26

if your login path is '/login'

in your App.vue

<template>
  <div id="app">
    <div class="routing">

    </div>
    <router-link to="/" v-if="$route.fullPath !== '/login'">Login</router-link>
    <router-link to="/home" v-if="$route.fullPath !== '/login'">Home</router-link>
    <router-view v-if="$route.fullPath === '/login'"></router-view>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  }
}
</script>

Upvotes: 0

Saurabh
Saurabh

Reputation: 73589

You can use v-if for this:

<router-link v-if="!isOnLoginPage()" to="/">Login</router-link>

where isOnLoginPage can be a simple method, which returns true or false depending on current route.

isOnLoginPage: function() {
  return this.$route.path === '/'
}

Upvotes: 3

Related Questions