将 axios 中的道具传递给 Vue.js?

Passing props in axios to Vue.js?

我想将属性从父组件传递到子组件。我的道具是tid.

这是父组件:

<div id="tracksec" class="panel-collapse collapse">
    <library :tid="track.category_id"></library>
</div>

这是子组件:

<script>
import Chapter from "./chapter";
import Http from "../../services/http/httpService";
export default {
    components: {Chapter},
    props:['tid'],
    name: "library",
    data(){
        return{
            library:{},
        }
    },
    computed:{
      getPr(){
          return this.$props;
      }
    },
        mounted(){
      console.log(this.$props);
        Http.get('/api/interactive/lib/' + this.tid)
            .then(response => this.library = response.data)
            .catch(error => console.log(error.response.data))
    }
}

这是 http 来源:

import axios from 'axios';


class httpService {

static get(url, params) {
    if (!params) {
        return axios.get(url);
    }
    return axios.get(url, {
        params: params
    });
}

static post(url, params) {
    return axios.post(url, params);
}
}

export default httpService;

我想将 tid 值传递给 http.get 函数。例如:

Http.get('/api/interactive/lib/' + this.tid)

但是 tid 的值为 undefined。 如何在挂载或创建的挂钩中获得 tid

在父组件中尝试此代码片段

<div id="tracksec" class="panel-collapse collapse">
  <library tid="track.category_id"></library>
</div>

然后在您的子组件代码中将如下所示:

<script>
  import Chapter from "./chapter";
  import Http from "../../services/http/httpService";
  export default {
    components: {
      Chapter
    },
    props: [ 'tid' ],
    name: 'library',
    data () {
      return {
         library: {},
      }
    },
    computed: {
      getPr () {
          return this.tid;
      }
    },
    mounted () {
      const tidValue = this.tid // get the value of props you've passed
      console.log(tidValue) // check if it is getting the right value.
      // code for http request.
    }
}
</script>

这是父脚本部分:

   import Library from "./library";
import Http from '../../services/http/httpService';

export default {
    components: {Library},
    name: "interactive",
    data() {
        return {
            track: {},
        }
    },

    mounted() {
        Http.get('/api/interactive/track/' + this.$route.params.id)
            .then(response => this.track = response.data)
            .catch(error => console.log(error.response.data))
    }
}

我是 Vue 的新手,但我认为您可能想要添加一个 "watcher" 来触发更改。您的 "track-object" 在创建时为空,此前导 track.category_id 未定义。然后,当您从 HTTP get 中获得答案时,您的值已设置,但不会在库组件中更新。

像这样:

<script>
import Chapter from "./chapter";
import Http from "../../services/http/httpService";
export default {
    components: {Chapter},
    props:['tid'],
    name: "library",
    data(){
        return{
            library:{},
        }
    },
    watch: {
        // if tid is updated, this will trigger... I Think :-D
        tid: function (value) {
          console.log(value);
            Http.get('/api/interactive/lib/' + value)
            .then(response => this.library = response.data)
            .catch(error => console.log(error.response.data))
        }
      },
    computed:{
      getPr(){
          return this.$props;
      }
    },
        mounted(){
      console.log(this.$props);

      // Will be undefined
      /*
        Http.get('/api/interactive/lib/' + this.tid)
            .then(response => this.library = response.data)
            .catch(error => console.log(error.response.data))*/
    }
}
</script>

Documentation about watchers

(无法测试代码,但您可能会明白)