Vue: props passed to router-link

只愿长相守 提交于 2019-12-11 06:48:15

问题


I want to have 3 main parts in my webapp:

  • App.vue - this page only has the <router-view> tag and some general configuration + it fetches an API every second
  • ControlPanel.vue - this page visualizes some data that the App.vue page gets
  • Profile.vue - this page visualizes some data that the App.vue page gets too

Right now I set up my App.vue with the API call and it passes the data it receives to the two pages with props like the following example. As you can see when it gets mounted it starts a loop that lasts 1 second where it goes and fetches the API and then it returns it to the two routes.

<template>
  <div id="app">
    <div id="nav">
      <router-link :to="{ name: 'control_panel', params: { APIlogs } }">Control panel</router-link>
      <span> | </span>
      <router-link :to="{ name: 'profile', params: { APIlogs } }">Profile</router-link>
    </div>
    <router-view/>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      APIlogs: '',
    };
  },
  mounted() {
    setInterval(() => this.refreshData(), 1000);
  },
  methods: {
    refreshData() {
      axios.get('http://192.168.30.65:5000/logs')
      .then((response) => {
          this.APIlogs = response.data;
      });
    },
    },
};
</script>
<style>
 ...
</style>

On the other hand, Control Panel and Profile are fundamentally the same page and they should get the props from the "father" and use it to visualize data but right now it doesn't work. When I click on one route it shows me the value the prop has in that moment and doesn't update as the App.vue page fetches more and more data.

<template>
  <div id="app">
    {{APIlogs}}
  </div>
</template>

<script lang="ts">
import axios from 'axios';

export default {
  name: 'control-panel',
  props: ['APIlogs'],
  data() {
    return {
    };
  },
  mounted(){
    console.log(this.APIlogs);
  },
  methods: {
  },
};
</script>

<style>
 ...
</style>

Did I do something wrong? Is my implementation good enough or is it lacking in some way? Really hope someone can help me out with this one, it's really tearing me apart.

Thanks a lot in advance

EDIT

Just to give a bit more context, before having props I was calling the same exact API from both components and it seemd very inefficient to me so I switched to this method.

Also my router.ts looks like this:

import Vue from 'vue';
import Router from 'vue-router';
import ControlPanel from '../src/components/ControlPanel.vue';
import Profile from '../src/components/Profile.vue';

Vue.use(Router);

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'control_panel',
      component: ControlPanel,
      props: true,
    },
    {
      path: '/profile',
      name: 'profile',
      component: Profile,
      props: true,
    },
  ],
});

回答1:


An easy way to make this work is to just make your APIlogs an object. Then it would be passed by reference and any updates to it will be reflected in the other components ..

export default {
  data() {
    return {
      APIlogs: {logs: ''},
    };
  },
  mounted() {
    setInterval(() => this.refreshData(), 1000);
  },
  methods: {
    refreshData() {
      axios.get('http://192.168.30.65:5000/logs')
      .then((response) => {
          this.APIlogs.logs = response.data;
      });
    },
    },
};

<template>
  <div id="app">
    {{APIlogs.logs}}
  </div>
</template>

PS: You should probably use clearInterval in your beforeDestroy hook.




回答2:


there's no params inside your paths i.e: path: '/:apilogs'

A dynamic segment is denoted by a colon :. When a route is matched, the value of the dynamic segments will be exposed as this.$route.params in every component.
(source)




回答3:


After a while and almost an entire afternoon wasted on this problem, I found out this article which helped me achieve my goal. I just created a file with all my api calls and I call it every time I need to fetch something. It's a way more elegant and intelligent solution I think.



来源:https://stackoverflow.com/questions/52874769/vue-props-passed-to-router-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!