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

307
Visualizações
Issue with router-link dynamic path using v-for loop in Vuejs?

//main.js

import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import HelloWorld from "./components/HelloWorld.vue";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [{ path: "/", component: HelloWorld }]
});

Vue.config.productionTip = false;

new Vue({
  router,
  render: (h) => h(App)
}).$mount("#app");
//User.vue

<template>
  <div>
    <form>
      <label>First name:</label><br />
      <input type="text" id="fname" name="fname" /><br />
      <label>Last name:</label><br />
      <input type="text" id="lname" name="lname" /><br /><br />
      <input type="submit" value="Submit" />
    </form>
  </div>
</template>

<script>
export default {
  name: "User",
};
</script>
//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: 'User', params: { id: item.id } }">
        {{ item.kk }}
      </router-link>
    </div>
    <br /><br /><br />
    <User />
  </div>
</template>

<script>
import User from "./User.vue";
export default {
  name: "HelloWorld",
  components: {
    User,
  },
  data() {
    return {
      items: [
        { id: 1, val: "1", kk: "mm" },
        { id: 2, val: "22", kk: "aa" },
        { id: 3, val: "55", kk: "dd" },
        { id: 4, val: "77", kk: "gg" },
      ],
    };
  },
};
</script>

Issue with router-link dynamic path using v-for loop in Vuejs?

I am getting error as below:- [Vue warn]: Missing required prop: "to" [Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading '_normalized')" TypeError: Cannot read properties of undefined (reading '_normalized')

Updated Code here:- https://codesandbox.io/s/suspicious-germain-vepmk?file=/src/App.vue

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your loop you access item.param which does not exist. I suppose you want to access item.id

<router-link :to="{ name: 'User', params: { id: item.id } }">
  {{ item.kk }}
</router-link>

A small additional remark - you should also set the id as key in your loop

<div v-for="item in items" :key="item.id">

EDIT

In your codesandbox you seem to have some errors (doesn't build due to missing import of User component in main)

However you will also need to use the route name preview as you defined it like this in your main.js

<router-link :to="{ name: 'preview', params: { id: item.id } }">
  {{ item.kk }}
</router-link>

AND even more important: You don't seem to define a router-view. Without a router-view your route component will not be displayed.

There seem to be many little mistakes here that are hard to sum up in one answer - please check the docs (https://router.vuejs.org/guide/#html) for the router-view

about 4 years ago · Juan Pablo Isaza Relatório

0

There are issues in your code

  • You have defined route name as 'preview' but using 'user' in your router-link
  • You are not using < router-view > tag which is used to show changed component basically you should use it in your app.vue

You should add router-view in your app or main component where you want to see change in component after routing. Also can you add some screenshots of what issues you are facing as it is not very clear from your question.

Edit: For removing # from url you should use hisory mode like enter image description here

about 4 years ago · Juan Pablo Isaza 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