What could be the reason behind my Vue 3 page not refreshing its content when navigating to a new page?

I am experiencing an issue with my Vue3 template file that is fetching data from Strapi. While it works fine on my local machine, the content only loads correctly on the first page load when I run it online. Subsequent page changes do not update the content (even though it's set up as a dynamic page). However, refreshing the page results in the correct content being displayed and the URL also updates accordingly.

<template>
  <div v-if="!pending">
    {{ route.params.slug }}
    <div v-if="project.data[0]">
      <Head>
        <Title>{{ project.data[0].attributes.title }} / Tuomoas Markunpoika</Title>
      </Head>
      <div id="hero">
        <img :src="`${config.public.strapiURL}${project.data[0].attributes.preview.image.data.attributes.url}`" />
      </div>
      <ContentBlocks :blocks="project.data[0].attributes.content" />
      <h2 v-if="project.data[0].attributes.items.data.length" class="main-heading">Collection</h2>
      <ItemGrid v-if="project.data[0].attributes.items.data.length" :overview="`projects`" :selection="`${route.params.slug}`" />
    </div>
  </div>
</template>

<script setup>
const route = useRoute()
const config = useRuntimeConfig()
const { data: project, pending } = await useAsyncData("project", () =>
  $fetch(`${config.public.strapiURL}/api/projects/?filters[slug][$eq]=${route.params.slug}&populate[meta][populate]=*&populate[preview][populate]=*&populate[content][populate]=*&populate[items]=*`)
)
if (!project.value || Object.keys(project.value.data).length === 0) {
  throw createError({ statusCode: 404, statusMessage: "Page Not Found" })
}
</script>

Answer №1

If you're looking to employ a watcher that triggers data re-requesting upon parameter changes, consider implementing something along these lines:

const route = useRoute()

// Refresh user information whenever parameters change
watch(
    () => route.params.slug,
    async (newSlug) => {
        // Perform data fetching
    },
);

For more details, check out: Accessing the Router and current Route inside setup

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

Scrolling to an id element in Vue.js can be achieved by passing the ID in the URL using the "?" parameter. This

My challenge involves a URL http://localhost:8080/?london that needs to load directly to the element with an id of london in the HTML section <section id="london"> on the page. Using http://localhost:8080/#london is not an option, even though it woul ...

How to disable swipe back feature in IOS on an Ionic 5 Vue application

In the past, I successfully implemented global swipe back disabling for an entire app using Ionic Angular with: IonicModule.forRoot({ swipeBackEnabled: false }) Alternatively, for a single page, I achieved this by injecting IonRouterOutlet: this.routerOut ...

VueJS - The instance does not recognize the property or method "currentUser"

In one of my components, I created an empty object called currentUser within the data() property. However, when trying to reference this object in the HTML template associated with the component, I encounter the following error: Property or method "curren ...

Displaying HTML content from a Vuejs response within a Dialog box

After receiving a response from the server via a REST request in HTML format, I have saved it in a data:[] variable. When I log this data on the console, it appears as raw HTML code. This reply is currently stored as a String, and my challenge now is to co ...

Preventing form submission on Enter in Vue.js while retaining input progress

This code snippet prevents enter key from submitting the form, but I want it to still work in input fields without submitting the form. <template id="form-template"> <form action="save-passport" @keypress.enter.prevent method="post" enct ...

Attempting to modify information in vue.js

I have been facing an issue with overriding data in the App.vue component. It seems that every time I try to update the data in my main.js file, the component still takes the default data. I am working with webpack as well. App.vue <template> & ...

What could be the reason behind the appearance of white lines in my code like this?

Does anyone have an idea why these random lines keep showing up in my code and won't go away unless I restart VScode? My OS is macOS v12.1, but I've been experiencing this issue since version 11 of the software. https://i.stack.imgur.com/lI0Fo. ...

Discovering ways to optimize argument type declarations in TypeScript

If we consider having code structured like this: function updateById( collection: Record<string, any>[], id: number, patch: Record<string, any> ): any[] { return collection.map(item => { if (item.id === id) { return { ...

Talwind Flexbox Grid Design

Currently, I am exploring the world of website layout creation using Tailwind Flexbox Grids as I believe it can add significant value. However, I have encountered some queries: How does one go about constructing a layout like this? Referencing this speci ...

What is the best way to style a value within a v-for loop inside an el-option element in Element UI?

I'm looking for a way to format the value in label(item.value) as a decimal within a v-for loop. Below is my code snippet: <el-form-item :label="label" :required="required" prop="Jan"> <el-select v-model=& ...

State properties in Vuex remain unchangeable despite using mutator methods

I've encountered an issue with the code in my vuex module called user.js: import Vue from "vue"; import Vuex from 'vuex'; Vue.use(Vuex); const state = { user: { firstName: null, lastName: null, ...

An error occurred in Vuejs: Type Error - _vm.moment is not a recognized function

Encountering a challenge while attempting to migrate to moment on Vuejs. Upon executing npm install vue-moment and adding the following script: <script> const moment = require('vue-moment'); ... </script> I included this in my & ...

Persistence of query parameters from old routes to new routes using vue-router

Whenever a query parameter called userId is present in a route within my application, I want the subsequent routes to also include this query parameter. Instead of manually modifying each router-link and router.push, I am looking for a solution using rout ...

Accessing and passing arguments to actions within Vuex: A how-to guide

Here is an example of one of my vuex actions. updateProds: (context, data) => { axios.get(data.url) .then((response) => { context.dispatch( ----- ); }) }, After receiving a response from the axi ...

Tips for boosting ViteJs development mode performance

One issue I am facing is the slow server performance during development mode. After starting the server and opening the page in my browser, I have to wait 3–6 minutes for it to load! Initially, ViteJs downloads a small amount of resources, but then the ...

Attempting to sort through an array by leveraging VueJS and displaying solely the outcomes

Within a JSON file, I have an array of cars containing information such as model, year, brand, image, and description. When the page is loaded, this array populates using a v-for directive. Now, I'm exploring ways to enable users to filter these cars ...

collapse each <b-collapse> when a button is clicked (initially shown)

I am facing an issue with a series of connected b-buttons and b-collapse, all initially set to be visible (open). My goal is to hide them all when a toggle from my parent.vue component is activated - so upon clicking a button in the parent component, I wa ...

Encountering "npm WARN optional dep failed, proceeding with fsevents" error while attempting to install vue-cli on AWS/EC2 instance

I'm in the process of setting up vue-cli on AWS. Everything seems good with my permissions, and I have node v4.4.5 installed. Upon executing npm install --global vue-cli, the cursor flashes for approximately 30 seconds before displaying this error m ...

When passing a prop to v-html, it may not render properly in Vue when dealing with SVG elements

I have a string that contains a sequence of elements as a prop, and I'm trying to render it using :v-html="prop": <template> <svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg" viewBox="0 ...

When the page is refreshed, the Vue prop briefly becomes undefined

Within my parent component, I am retrieving a todo item from a pinia store based on the current vue router params /todo/123. This todo item is then passed into a child component as a prop. Normally, the app functions correctly when navigating to the compo ...