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

问题描述 投票:4回答:3

我想将道具从父组件传递到子组件。我的道具是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

javascript laravel vuejs2 axios
3个回答
2
投票

我是Vue的新手,但我想你可能想要添加一个触发变化的“观察者”。您的“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

(不能测试代码,但你可能会得到这个想法)


1
投票

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

<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>

0
投票

这是父脚本部分:

   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))
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.