Moh*_*hNj 5 javascript laravel axios vuejs2
我想将道具从父组件传递到子组件。我的道具是tid。
这是父组件:
<div id="tracksec" class="panel-collapse collapse">
<library :tid="track.category_id"></library>
</div>
Run Code Online (Sandbox Code Playgroud)
这是子组件:
<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))
}
}
Run Code Online (Sandbox Code Playgroud)
这是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;
Run Code Online (Sandbox Code Playgroud)
我想将tid值传递给http.get函数。例如:
Http.get('/api/interactive/lib/' + this.tid)
Run Code Online (Sandbox Code Playgroud)
但是tid值是undefined。如何tid进入已安装或已创建的挂钩?
我是 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>
Run Code Online (Sandbox Code Playgroud)
(无法测试代码,但你可能会明白)