Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

385
Views
In VueJS, how can I generate dynamic background images based on the Route?

Is there a way in VueJS to create dynamic backgrounds based on the Route? For instance, I have the followings routes:

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/:col",
    name: "collections",
    component: () =>
      import(
        /* webpackChunkName: "collections" */ "../views/CollectionsView.vue"
      ),
    children: [
      {
        path: "staked",
        name: "staked",
        component: () =>
          import(/* webpackChunkName: "staked" */ "../views/StakedView.vue"),
      },
      {
        path: "unstaked",
        name: "unstaked",
        component: () =>
          import(
            /* webpackChunkName: "unstaked" */ "../views/UnstakedView.vue"
          ),
      },
    ],
  },
];

I would like to set a different background image for each of the dynamic routes under the named route "collections", so "/collection1/..." would display a different background image than "/collection2/.

Here is my template inside AppView.vue:

<template>
  <div class="background">
    <div
      :style="{
        backgroundImage: image,
      }"
    >
      <TheAppHeader />
      <div class="main m-auto">
        <TheTitle />
        <router-view />
      </div>
    </div>
  </div>
</template>

I don't know how I can grab that collection name from the route and use it to get the desired background image into "".

My background images are all in the /src/assets/ directory.

Any ideas? Thank you!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use watch property for this

watch: {
  '$route.params'(currentParam) {
    if (currentParam == '/collection1') {
      this.image = <put your image here>
    }
    else if (currentParam == '/collection2') {
      this.image = <put your another image here>
    }
  }
}

Please use to colsole current param to check your required/target values

about 4 years ago · Juan Pablo Isaza Report

0

You can pass the path to your background image as a prop to a route.

// In routes.js
const routes = [{
    path: "/",
    name: "home",
    component: HomeView,
    props: {
        backgroundImage: './src/assets/route-backgrounds/home.jpg',
    },
  }]

// In component
  props: [ "backgroundImage" ],

// In template
<div :style="{ backgroundImage: `url('${backgroundImage}')` }"></div>
about 4 years ago · Juan Pablo Isaza Report

0

Can use $route.params like this.

watch: {
  '$route.params'(currentParam) {
      this.image = `assets/images/${currentParam}.jpg`
    // i.e. assets/images/collection1.jpg

  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!