Conceal the Vue router on a webpage

How can I hide the vue-router from my login page? I want to remove the menu on the Login page. Is it possible and how would I achieve that?

Here is the code for the Login page:

Login

<template>
 <div>
     <h1>Login</h1>
        <form action="">
            <label>Name</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>

https://i.sstatic.net/elIga.png

Answer №1

After encountering the same issue, I discovered two potential solutions:

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

The concept involves using App.vue as a placeholder for elements shared across all pages (which may be none in your case) and inserting

<router-view></router-view>
within it. For simplicity, this could look like:

//App.vue

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

Additionally, you would need another template to contain 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>

Your router should have configuration similar to:

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 setup will display the login page at / and other pages with the menu at /restricted/{other_page}. By implementing this method, the menu will not be rendered.

2. Applying v-if on components to control rendering.

In your App.vue, use v-if="this.$route.path !== '/'" on elements that shouldn't render, such as the router-links. You can enclose them and apply v-if="this.$route.path !== '/'" to the containing 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>

Best regards.

Answer №2

If you want to conditionally show a link based on the current route, you can achieve this using v-if:

<router-link v-if="!isHomePage()" to="/">Home</router-link>

In this case, the method isHomePage determines whether the user is currently on the home page or not.

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

Answer №3

if the path for logging in is '/login'

within your App.vue file

<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Utilizing VueJS XHR functionality within a versatile and reusable component

Seeking advice on best practices for improving the following scenario: I have a single global reusable component called <MainMenu>. Within this component, I am making an XHR request to fetch menu items. If I place <MainMenu> in both the heade ...

Javascript and Codeigniter interaction: Using conditionals with Ajax

I am having trouble understanding this code snippet. I am currently studying Ajax and came across this piece of code that automatically inserts data. However, I am unsure about the line if(result=='12') then trigger ajax. What does the number 12 ...

Tips for triggering the button command event using JavaScript

Is there a way to activate the button command event using JavaScript? I'm not referring to the BUTTON onclick event. ...

What is the method to display HTML code using JavaScript within a span element in a JSP page?

While working on a jsp file, I came across a span tag with the following code: <span id="divBasicSearchResults" style="height:100%;"></span> Below is the Javascript function that generates HTML content for this span: function renderBasicSear ...

Dropdown search menus are failing to appear in the correct location

I have 4 dependent search dropdown menus side by side. There are two issues I am facing: Whenever I type in any of the drop-down menus, the MySQL-connected lists appear but not directly beneath the actual 'input-type-search-box'. Additional ...

Organizing Vue Components in Laravel Without Single Page Application Approach: Best Practices for Separate File Structure for Each Page

I have a Laravel Vue application that is not a single page application (SPA), so I am still using Laravel blades to separate the pages. Each page imports app.js, which is compiled by webpack and contains all my Vue components. The issue is that app.js is b ...

Is there a discrepancy in speed between Node.js http.get and Google Chrome's $.get function?

Recently, while experimenting with node.js, I decided to test out the following code snippet: var http = require("http"); function get() { var headers = { 'Accept-Encoding': 'gzip' }; var startedAt = new Date().get ...

Implementing a Tri-state Checkbox in AngularJS

I've come across various discussions on implementing a 3-state checkbox with a directive or using CSS tricks (such as setting 'indeterminate=true' which doesn't seem to work). However, I'm curious if there's another method to ...

Using PHP to iterate through an array and output the values within a

Hey there! I have a question about incorporating PHP foreach and echo into JavaScript to make it dynamic. Here is the current static JavaScript code: <script type="text/javascript"> $(document).ready(function(){ $('input[type="checkbox"]&ap ...

Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One o ...

Retrieving data from a dynamically-created Ajax form

I am encountering an issue that I need help with. Using AJAX, I have generated a table dynamically which includes a form with checkboxes for item checking. Here is a snippet of the code: <form name="formdocs"> Followed by: <input type="checkbo ...

Encounter issues with v-for functionality when using Internet Explorer 11

I am facing an issue with the rendering of a table in a simple Vue instance. The document displays correctly on Firefox and Chrome, however, I encounter an error in IE11: [Vue warn]: Error when rendering root instance. I assumed that Vue is compatible ...

AngularJS Alert: [$injector:unpr] Provider Not Recognized

After setting up the URL routes for the sportsStore app from an AngularJS book to learn, I'm encountering the following errors: Error: [$injector:unpr] Unknown provider: $templateRequestProvider <- $templateRequest <- $route <- ngViewDirect ...

Send the window to Google before submitting a search using Google

I've successfully sent a query to Google using window.location.href, but I'm stuck on how to submit the search form to view the results. I have my javascript code that directs the window to Google with the query from the search-box, but I'm ...

What is the best method for sending the styled and checked option via email?

Just finished customizing the checkboxes on my contact form at cleaners.se/home. Here's my question: If I select "telefon," how can I ensure that this option is sent to my email? In Contact Form 7, I used to simply type a shortcode. Is there a simila ...

No Access-Control-Allow-Origin or Parsing Error with jQuery

I am attempting to use ajax from a different server to request data from my own server as a web service. The response is a valid json generated by json_encode. {"reference":"","mobile":"","document":"","appointment":""} To avoid the 'Access Control ...

Ways to create an object based on another object

Currently, I am teaching myself Javascript. My focus is on working with Vuejs and express for a CRUD App. In one of my components, I retrieve data from my backend using the API: localhost:3000/api/transport. The response contains all objects from the data ...

Submit the form only when the specified conditions are met, otherwise return

Is there a way to submit a form after it has been prevented from submitting? I've searched for solutions on StackOverflow but haven't found one that works for me. Below is the code snippet in question: $(function(){ $("#loginform").submit(f ...

Adding custom styles to Material-UI components

` import React, { Component } from 'react'; import Header from './Components/UI/header'; import { Box, Paper } from '@material-ui/core'; import { ThemeProvider, withStyles} from '@material-ui/core/styles'; const ...

Utilize the Webstorm debugger in conjunction with node.js for seamless debugging

Several weeks ago, I attempted to get the webstorm debugger up and running, but unfortunately, it didn't work. Now, I'm giving it another shot, but I'm faced with the same outcome. I am following the instructions outlined here: http://www.j ...