Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
How to reset router-link id in Vuejs?

HelloWorld.vue

<template>
  <div>
    <b>Vuejs dynamic routing</b>
    <div v-for="item in items" :key="item.id">
      <b>{{ item.id }}.</b> &nbsp;&nbsp;&nbsp;
      <router-link :to="{ name: 'UserWithID', params: { id: item.id } }">
        {{ item.kk }}
      </router-link>
    </div>
    <br /><br /><br />
    <User />
  </div>
</template>

<script>
import User from "./User.vue";
import { datalist } from "./datalist";
export default {
  name: "HelloWorld",
  components: {
    User,
  },
  data() {
    return {
      items: datalist,
    };
  },
};
</script>

datalist.vue

export const datalist = [
  { id: 1, val: "11", kk: "potter" },
  { id: 2, val: "22", kk: "james" },
  { id: 3, val: "55", kk: "limda" },
  { id: 4, val: "77", kk: "stepen" }
];
 

main.js

import VueRouter from "vue-router";
import HelloWorld from "./components/HelloWorld.vue";
import Vuex from "vuex"
import createPersistedState from "vuex-persistedstate";

Vue.use(VueRouter);

const store = new Vuex.Store(
  {
    plugins: [createPersistedState({

      storage: window.sessionStorage,
    })],
    state: {

    },
    mutations: {
      // setAuthentication(state, status) {
      //   state.authenticated = status;
      // }
    }
  }
);

I want to reset the router-link id. the issue is like, When I click on router-link, in the URL section, I can see pathname/component name followed by id.

When I click on the back button every time I can see my action like:

/1../2 .../3

In the URL section. I want to reset the value when clicking on the back button. and on page refresh array from v-for is getting cleared.

Working code:- https://codesandbox.io/s/autumn-cherry-p5s9b?file=/src/main.js

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem with your code is that you did not use history mode for the router, I highly recommend this mode because it resets the router, you cannot do anything about it in the helloworld vue, you have to go to your router file and add/change this code:

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

I hope this will work for you, this will also remove the # in your route.

If this still does not work, you can try creating a new vue project with the router set to history mode.

about 4 years ago · Santiago Trujillo Relatório

0

I implemented and tested the functionality that you need.

The Requested Solution

<template>
  <div>
    <b>Vuejs dynamic routing</b>
    <div v-for="item in items" :key="item.id">
      <b>{{ item.id }}.</b> &nbsp;&nbsp;&nbsp;
      <router-link :to="{ name: 'UserWithID', params: { id: item.id } }">
        {{ item.kk }}
      </router-link>
    </div>
    <br /><br /><br />
    <User />
  </div>
</template>

<script>
import * as $ from 'jquery'
import User from './components/User.vue'
export default {
  name: 'HelloWorld',
  components: {
    User
  },
  data () {
    return {
      items: [
        { id: 1, val: '11', kk: 'potter' },
        { id: 2, val: '22', kk: 'james' },
        { id: 3, val: '55', kk: 'limda' },
        { id: 4, val: '77', kk: 'stepen' }
      ]
    }
  },
  methods: {
    BackFunction () {
      console.log('back button clicked !')
      window.location.href = '/'
    },
    RefreshFunction () {
      console.log('reload button clicked !')
    }
  },
  mounted () {
    let BF = this.BackFunction
    $(window).on('popstate', function (e) {
      BF()
    })
    let RF = this.RefreshFunction
    $(window).on('unload', function (e) {
      RF()
    })
  },
  beforeDestroy () {
    let BF = this.BackFunction
    $(window).off('popstate', function (e) {
      BF()
    })
    let RF = this.RefreshFunction
    $(window).off('unload', function (e) {
      RF()
    })
  }
}
</script>

When the back button is clicked the URL is refreshed.

My Recommended Solution

Use the replace property of the router-link component Here

Setting replace prop will call router.replace() instead of router.push() when clicked, so the navigation will not leave a history record.

When the history is not recorded, no need for the back button event listening.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda