Rumah  >  Soal Jawab  >  teks badan

Cara menghantar parameter URL dalam pautan penghala dalam VueJS

Saya cuba menghantar permintaan kepada '/trackers/:id/Update' atau '/trackers/:id/Delete' daripada halaman Trackers dalam VueJS, masalahnya ialah id saya berada dalam data yang dipanggil item yang saya Saya dapatkan daripada Untuk mendapatkan API dan menyimpannya secara setempat untuk paparan, saya perlu mendapatkan {{item.trackerid}} dalam URL sasaran, iaitu "/trackers/:id/Delete". Saya tidak dapat mencari jalan. Bolehkah seseorang memberitahu saya jalan yang betul?

TrackersView.vue:

<template>
    <h1>Hello {{name}}</h1>
    <h3>This is your Trackers page</h3>
    <table class="table table-striped">
  <thead>
    <th>Sno</th>
    <th>Tracker name</th>
    <th>Tracker Description</th>
    <th>Tracker settings</th>
    <th>Date Created</th>
    <th>Add tracker log</th>
    <th>Actions</th>
  </thead>
  <tbody>
<tr v-for="item,index in items" :key="index">
                <th>{{index}}</th>
                <th>{{item.trackername}}</th>
                <th>{{item.trackerdesc}}</th>
                <th>{{item.tracker_settings}}</th>
                <th>{{item.datecreated}}</th>
                <th><button class="btn btn-outline-dark">+</button></th>
                <th><button class="btn btn-outline-dark"><router-link :to="{ name: 'tupdate', params : {id : localStorage['id'] } }" style="text-decoration:none; color: black;">Update</router-link></button>
                </th>
                <th>  <button class="btn btn-outline-dark">  <router-link to="/trackers/{{item.trackerid}}/Delete" style="text-decoration:none; color: black;">Delete</router-link></button></th>
                
            </tr>
  </tbody>
</table>


<button class="btn btn-outline-dark" @click="createtracker">Click to add trackers</button>
</template>

<script>
import { useRouter } from 'vue-router';
import axios from 'axios';
export default {
Name: 'TrackersView',
mounted() {
    let id = localStorage['id'] 
    return{
        id,
    }
},
data() {
    return {
        name: localStorage.name,
        items : [],
        
    }
},
methods:{
  getTrackers(){
    const path = 'http://localhost:5000/trackers/'+ localStorage['id'] 
    axios.get(path)
    .then((res) => {
      this.items = res.data.tracker
  })
 
    .catch((err) => console.log(err.response))

    console.log(this.items);
  }
},
setup() {
  const router = useRouter();
  const createtracker = async () => {
    await  router.push({name: 'createtracker',params : {id : localStorage['id']}});
    };
    return{
        createtracker,
    }
},
created() {
  this.getTrackers();
}
}
</script>

<style scoped>
    body {
    background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
    background-size: 400% 400%;
    animation: gradient 15s ease infinite;
    height: 100vh;
  }
  
  @keyframes gradient {
    0% {
      background-position: 0% 50%;
    }
    50% {
      background-position: 100% 50%;
    }
    100% {
      background-position: 0% 50%;
    }
  }
</style>t

Keputusan akhir adalah ini:

Inilah yang terdapat dalam localStorage:

P粉930448030P粉930448030305 hari yang lalu552

membalas semua(1)saya akan balas

  • P粉106301763

    P粉1063017632023-12-20 00:53:22

    Pertanyaan untuk laluan semasa adalah dalam ini.$route.query, jika anda ingin menyimpannya di destinasi anda boleh menghantar keseluruhan objek pertanyaan ke laluan lain:

    <router-link
    
    
    :to="{ path: '/', query: $route.query }"
    >
      Go There
    </router-link>

    Jika anda hanya mahu lulus parameter pertanyaan tertentu:

    <router-link
      :to="{ path: '/', query: { myQuery: $route.query.myQuery }}"
    >
      Go There
    </router-link>

    balas
    0
  • Batalbalas