Guide on developing a dynamic Navbar component on Laravel Mix with Vue Js

As I delve into learning and practicing Vue.js, my goal is to create a component that includes elements like Navbar and Footer within a single view. Initially, I set up an index for the first view but faced an issue with adding the navbar as it did not show in the index view. Here is my folder structure:

Resource 
    *js
        *spa
            *IndexComponent
            *HeaderComponent
        *app.js
        *App.vue
        *boostrap.js

Within my IndexComponent file, here is how I structured my code:

<template>
   <div class="container.is-fullhd"> 
    <section class="section">
    <div class="container is-fluid">
    
      <!-- Rest of the table content goes here -->

    </div>
     </section>


    </div>
    </template>

I utilize Vue Router to navigate between pages, where App.vue plays a role in displaying the content. Below is the template used in App.vue:

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

    <script>
    export default {
    }
    </script>

Here are snippets from my app.js and bootstrap.js files:

require('./bootstrap');

 window.Vue = require('vue');

  import VueRouter from 'vue-router';
  Vue.use(VueRouter);

  import VueAxios from 'vue-axios';
  import axios from 'axios';

  // Importing components
  import IndexComponent from './components/spa/IndexComponent.vue';
  import HeaderComponent from './components/spa/HeaderComponent.vue';
  import FooterComponent from './components/spa/FooterComponent.vue';
  import AboutComponent from './components/spa/AboutComponent.vue';

  // Defining routes
  const routes = [
      {
          name: 'index',
          path: '/',
          component: IndexComponent
      }, 
      {
          name: 'about',
          path: '/about',
          component: AboutComponent
      },    

   ];

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

   // Mounting the app
   const app = new Vue(Vue.util.extend({ router }, App)).$mount('#app');

My question now is, how can I ensure that the navbar remains visible across all views, including when navigating away from the index? Any suggestions on how to achieve this?

Answer №1

To ensure that the Navbar remains visible regardless of the route change (for example, switching from "Index" to "About"), simply include your Navbar component (or HTML code) at the same level as your <router-view> in the App.vue file:

<template>
  <!-- Your navbar component/element -->
  <Navbar />
  <!-- The component linked by vue-router (e.g. "Index" or "About") -->
  <router-view></router-view>
</template>

<script>
export default {
}
</script>

This setup ensures that only the content within <router-view> will be refreshed when navigating, while elements/components outside of it remain visible on the screen.

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

The command 'php artisan migrate' encountered an access denied error for user 'homestead' at IP address '172.21.0.1'

After running the command php artisan migrate, I encountered an issue (as shown in the screenshot below) view image here To troubleshoot, I attempted the following steps: composer dump-autoload -o php artisan cache:clear php artisan config:clear php art ...

Utilizing D3 for translation by incorporating data from an array within d3.js

I need assistance with drawing rectangles in an SVG where the translation data is stored in an array. Below is the code: var vis=d3.select("#usvg"); var rectData=[10,21,32,43,54,65,76,87,98,109]; vis.selectAll("rect").data(rectData).enter().append("rect ...

Even with e.preventDefault, the anchor link still opens in a new tab on the browser

I am dealing with an ajax call that triggers a REST API to return a list of all names, as well as another REST API that matches those names. For instance: /list returns: list1, list2, list3 and /api/list1.json returns: JSON data for list1.. Currently, ...

In the callback function within Array.prototype.map, make sure to access the newly created array

Array.prototype.map() creates a new array. How can I use this new array within the callback function passed to Array.prototype.map()? For instance: someArray.map(function(item, idx, arr) { return { theCreatedArray: xyz }; }); What should I assign to ...

Having trouble with downloading files using Laravel Excel?

There seems to be a similar issue described in this post Laravel Excel Download using Controller It's surprising that there isn't a straightforward method to handle Excel downloads in Laravel without resorting to another resource. I have success ...

What steps should I take to create a functional image scrolling effect similar to the one shown in this example?

Recently, I came across and was intrigued by the "image slide effect" featured on their homepage. The way the background image changes as you scroll up, leading you through different introductory slides before reaching the main content of the site caught ...

Determine the current iteration index within recurring tasks using BullJS

When working with repeatable job callbacks, I often need to perform specific actions at a certain point in the script. For instance: const Bull = require('bull'); const queue = new Bull('payment'); // This task should run every 5 minut ...

Troubleshooting NodeJS and Express: Issue accessing a function located outside a folder

I'm having trouble accessing the function I exported in app.js Here is the code snippet from app.js: function getConnection() { return mysql.createPool({ host: 'localhost', user: 'root', password: &apo ...

Issue with Jquery animation persists: mouse cursor does not change and style remains unchanged even after clicking

I have encountered an issue with JQuery animation on Chrome. According to the requirements, I need to animate a div when a link is clicked. When the cursor hovers over the link, it should be underlined and change to a pointer. However, even after clickin ...

Gathering the presently unfinished observables within a superior-level rxjs observable

As an illustration, let's consider a scenario where I have a timer that emits every 5 seconds and lasts for 10 seconds. By using the scan operator, I can create an observable that includes an array of all the inner observables emitted up until now: c ...

What is the best way to save a PDF from within a frame using JavaScript and an HTML5 <embed> tag?

I'm looking for assistance with a script for my website that dynamically generates a PDF after the user makes selections in one of the frames. The website uses the HTML5 tag to display the PDF file. Can anyone provide guidance on a script that can: ...

Variety of properties determined by a "type" prop, expanding variations based on a value from the interface

I am trying to enhance a type based on a value from the main interface. If the type == multiline, it will have a specific interface, and if the type == icon, it will have a different type. import React, { memo, useCallback, ReactNode } from 'react&apo ...

How can we use JavaScript to close a dropdown menu when the user clicks outside of it?

I am facing an issue with my code. I want to close the dropdown menu when clicking outside of it or on the items within the dropdown. How can I achieve this? I attempted to use addEventListener('click', myFunction) on `document` but it did not w ...

How can one go about incorporating the feature "updating list upon clicking a label" on a webpage?

On my website, I currently display a comprehensive list of publications. However, I am looking to organize these publications by various research topics using labels. Ideally, when clicking on a specific label, only the papers related to that topic will be ...

Executing a JavaScript function with jQuery

function launch() { $("p").text("Hey there", greet()); } function greet() { alert("Greetings! You have triggered another function"); } HTML: <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button onclic ...

Issue with custom validator in Angular 6: setTimeout function not functioning as expected

Currently, I am in the process of following a tutorial to implement Asynchronous validation in Angular. The goal is to create a custom validator named shouldBeUnique that will be triggered after a 2-second delay. To achieve this, I have utilized the setTim ...

Sending variable boolean values to a VueJS component

How can I assign dynamic properties to a VueJS Component using VuetifyJS? Below is an example of VuetifyJS code that constructs a select field element: <div id="app"> <v-app id="inspire" style="padding: 10px; "> ...

How can I trigger an event in Vue.js when a selection is made in a dropdown menu?

Here is an illustration fiddle: https://jsfiddle.net/40fxcuqd/ Initially, it shows the name "Carl" If I choose Carol, Clara, etc., an event will be triggered and data will be logged to the console. However, if I click on the dropdown and select "Carl" ...

Passing Node.js MySQL query results to the next function within an async.waterfall workflow

In my node.js code using express, I have set up a route to request data from a mysql database. My goal is to pass the returned JSON in tabular form to another function to restructure it into a hierarchy type JSON. I have individually tested the script to ...

How can we properly access the DOM element generated by an {#each} loop in Svelte when it is clicked?

Using Svelte's {#each} functionality, I am reactively loading elements in the following way: {#each $items as item} <div> <Button on:click={someFunction}>{item.text}</Button> (*) </div> {/each} (*) a component t ...