简体   繁体   English

Vue.js router-link 数据绑定

[英]Vue js router-link Data bind

If I need to summarize what I want to do, when the post page is logged, data is drawn from the database with axios and listed.如果我需要总结我想做post事情,当记录页面时,数据从数据库中提取 axios 并列出。 and then when router-link is clicked or via Url domain.com/post/1 Automatically, when typed 2 axios are made on demand.然后当单击router-link或通过 Url domain.com/post/1自动输入 2 axios 时按需生成。 But what I want to do is When you log in to the post page, the data is already listed and I think that there is no need to make an axios request when you want to provide a detail entry from the extra.但我想做的是当你登录到post页面时,数据已经列出,我认为当你想从额外提供详细条目时,没有必要发出 axios 请求。 In this case, I want to send data over the route, but I couldn't figure out how to do it.在这种情况下,我想通过路由发送数据,但我不知道该怎么做。 I can bind with the @click function, but I want to do it via the routing without the click that I want done.我可以与@click function 绑定,但我想通过路由来完成它,而不需要我想要完成的click I want it to be bind directly when domain.com/post/1 is written on the url. Thank you in advance for your answer.我希望在url上写domain.com/post/1时直接绑定。先谢谢你的回答。 Router路由器

import Post from "./website/Post.vue";
import PostDetail from "./website/PostDetail.vue";
export default [
  {
    path: 'Post',
    component: Post,
    name: 'post',
    children: [
      {
        path: ':id',
        name: 'PostDetail',
        component: PostDetail,
        props: true
      }
    ]
  },
]

Post.Vue Post.Vue

<template>
 <div v-for="(list,index) in state.postList" :key="index">
  <router-link :to="{name: 'PostDetail', params: {id: list.id}}"/>
 </div>
 <div class="child">
  <router-view/>
 </div>
</template>

<script setup>
import axios from "axios";
import {reactive} from "vue";

const state = reactive({
 postList: [],
});
const fetchList = () => {
 axios.get("/post")
  .then((response) => {
   state.postList = response.data;
  });
};
fetchList()
</script>

PostDetail.vue PostDetail.vue

<template>
 <div>
  {{ getData }}
 </div>
</template>

<script setup>
import axios from "axios";
import {ref, defineProps} from "vue";

const props = defineProps({id: String})
const getData = ref([])

const getDatabase = () => {
 axios.get('/post' + '/' + props.id + '/detail')
  .then((response) => {
   getData.value = response.data;
  })
};
getDatabase()
</script>

Sorry for typing on Answer area haven't 50 reputation.很抱歉在答案区打字没有 50 声望。 You can try using onMounted to get your getDatabase when the page load/reload will direct calling the method.当页面加载/重新加载将直接调用该方法时,您可以尝试使用onMounted来获取您的 getDatabase。 For more detail read Lifecycle Hooks有关更多详细信息,请阅读Lifecycle Hooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM