Setting Vuetify component props dynamically depending on certain conditions

I've implemented a navbar component in my web app using the v-app-bar Vuetify component. However, I'm facing an issue where I need to dynamically set the props of the v-app-bar based on the current page the user is viewing.

<v-app-bar
  absolute
  color="#43a047"
  dark
  shrink-on-scroll
  prominent
  src="https://picsum.photos/1920/1080?random"
  fade-img-on-scroll
  scroll-target="#scrolling-techniques-5"
  scroll-threshold="500"
>

For example, the code snippet above showcases a standard v-app-bar component with props that allow for an image background which fades and shrinks as the user scrolls down. This design is specifically intended for the landing page. I'd like to have a unique style for the navbar when users navigate to other pages on the site. While I've experimented with creating separate navbar components or using v-if statements within the component to handle this, it has led to code duplication and inefficiency. Given that I am working on a large-scale website with diverse user accounts, it would be much more convenient if I could dynamically adjust the Vuetify props of the v-app-bar based on the specific page being viewed rather than creating multiple variations of the navbar component.

Answer №1

If you are utilizing the v-app-bar in your layout, it seems that directly accessing the route using this.$route may not be the most efficient approach due to the need for conditional statements or a cumbersome switch statement.

In Nuxt, each route is associated with a specific page file where details can be defined and passed to the layout as props for the v-app-bar component. One way to achieve this is by utilizing an event bus. I have previously explained its usage in another answer found here, but I can provide a summary of how to implement it in this scenario.

It appears that you intend to customize the navigation bar by passing props such as color, image source, scroll target, etc., from your page. The following example demonstrates how you can pass these props:

//index.vue -- or any route 

export default {
  mounted() {
      let props = {
        color: '#43a047',
        src: 'https://picsum.photos/1920/1080?random',
        scroll-target: '#scrolling-techniques-5',
        scroll-threshold: 500
      }
      this.$nuxt.$emit('appBarProps', props);
  }
}

To receive the emitted data from the event bus in your layout, you can listen for it like this:

//default.vue

created() {
    this.$nuxt.$on('appBarProps', p => {
      this.appBarProps = p;
      })
    },

data() {
    return {
      appBarProps: {}
    } 
}

beforeDestroy() {
    this.$nuxt.$off('appBarProps');
},

Finally, utilize the received appBarProps object within your v-app-bar component for customization.

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

Encountering parameter issues while working with Google Maps React in TypeScript

Currently, I'm utilizing TypeScript in this particular file. import React, {Component} from 'react' import {Map, InfoWindow, Marker, GoogleApiWrapper, mapEventHandler, markerEventHandler} from 'google-maps-react'; import { coordina ...

Retrieving a specific object from a subarray using MongoDB's ID

My MongoDB document includes the following: const Categories = [ { _id: 's654fs54s6d4f' title: 'category 1', SubCats: [ { _id: 'jhgfsf68746' name: 'subcat 1', i ...

What is the best way to align the variables in this JavaScript file with the variables in Flask/Python?

I have a JavaScript file that enables dynamic drop-down menus (i.e. selecting one option affects the others). However, I hard-coded the starting variables in this file to HTML elements 'inverter_oem' and 'inverter_model_name'. Now, I ne ...

A plethora of unhandled DOMException errors found in a basic Vue.js application

I have been working on a Vue.js project for learning purposes, incorporating Quasar and Vuex. However, I keep encountering numerous Uncaught (in promise) DOMException errors which appear in the console. The application seems to work fine, but these red err ...

A guide to integrating gatsby-remark-images-grid in markdown with Gatsby.js

Currently, I am working on creating a blog using Gatsby.js with markdown and I want to incorporate CSS grid into my blog posts to showcase a beautiful grid layout of images. I am aware that Gatsby offers incredible plugins to address various challenges. ...

Jquery adds a fun, wobbly effect to animations

I'm experiencing an issue with the animation I've implemented causing some slight shaking and wobbling of the text and certain elements which is affecting the overall look. You can view a live example of this behavior here: This problem specific ...

Troubleshooting Vue v-for loop triggering click events on multiple items simultaneously

After conducting a thorough search, I haven't found a suitable answer for my issue. The problem I am facing involves a v-for loop with buttons on each item, using VueClipboard2 to copy text. Whenever a button is clicked, some CSS changes are applied t ...

In my chat application, I encountered the error message "Received an expression instead of an assignment or function call - no-unused-expressions"

While working on my React Chat app and trying to access my Firebase, I encountered the error message: "Expected an assignment or function call and instead saw an expression no-unused-expressions" The error seems to be related to the assignment of this.rem ...

Set the value of an input without using v-model or any form of data binding

In the v-for loop, I want to populate some input fields with predetermined text without attaching them to the object. My current attempt looks like: <div v-for="item in allItems"> <input type="text" class="header-title" value="item.name">< ...

What is the best way to retrieve the current CSS width of a Vue component within a flexbox layout grid after it has been modified?

There's something about this Vue lifecycle that has me scratching my head. Let me simplify it as best I can. I've got a custom button component whose size is controlled by a flex grid container setup like this: <template> < ...

What is the process for customizing the appearance of a prop within the <TextField> component?

Using my custom DatePicker.js component, I have two instances of <TextField>. When the user clicks on the <CalendarIcon> within the <TextField>, a small calendar pops up. However, I want to style the label in the <TextField> in a sp ...

The JavaScript code appears to have malfunctioned as it abruptly terminates with an exit code of 1

When running the code, it reports that prompt-sync cannot be found and exits with error code 1 const input = require('prompt-sync')(); var firstName = input("Enter your first name:"); var lastName = input("Enter your last name:"); console.log(" ...

Automatically assigning IDs to all elements on an Angular webpage

We are currently developing an enterprise web application and have brought on board an e2e test engineer to automate tests. The test engineer has requested that we assign IDs to all elements in our pages. Is there a tool or method available to automatical ...

Is it necessary to include Babel when integrating Webpack?

After reading an informative article, it is highlighted that Babel’s plugin-syntax-dynamic-import plays a crucial role in enabling lazy loading. Without this, the syntax const AppHome= () => import("@/components/AppHome"); will not be compiled by webp ...

Typescript method fails to compile due to an indexing error

Imagine you're trying to implement this method in Typescript: setResult(guId: string,fieldname: string, data:Array<UsedTsoClusterKey>) { let octdctruns: OctDctRun[] = [...this.octDctRuns]; const index = octdctruns.findIndex((o) => o.guid ...

Using jQuery to create tabs and display or hide tab-like content

I'm looking for a simpler way to achieve my goal without using the jQuery-UI library. Here is the setup, where I have navigation markup in the header: <ul> <li><a class="active" href="#" title="">Uno</a></li> <li& ...

How to display and download a PDF file on a JSP web page

Can anyone help me with a simple trick to solve this question I have? In my project folder, I have 5 PDF files named file1.pdf, file2.pdf, file3.pdf, file4.pdf, and file5.pdf. I want to display these files in 5 rows on my webpage, each row containing VIEW ...

Is there a way to retrieve the content of an element using JavaScript?

I'm currently working on retrieving the value and content of an option element. I've managed to obtain the value using this.value as shown in the code snippet below: <select name='name' id='name' onchange='someFunctio ...

What is the best way to reference an ImageButton using jquery?

I created an HTML page with a special div in the body that contains a collection of buttons and images. <div id="div_one"> <button id="button_one"> <asp:Image id="image_button_one" ImageUrl=".." runat="server"> </button> </div& ...

When clicking on the file input field in Angular.js, the image name mysteriously disappears

I am currently utilizing ng-file-upload to upload images with Angular.js. The issue I'm encountering is that when a user selects a file for the second time in the same field, the name of the previously chosen image doesn't display. Below is my c ...