javascript - vuejs父组件如何向子组件传值?
本文介绍了javascript - vuejs父组件如何向子组件传值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
如题,我有一个List.vue
,里面包含了两个子组件,tab
和pagination
,这个List.vue
的数据是从接口得来的,从接口的来的数据分别是categories
, tabs
, active
,它们三个都是对象,我现在需要在子组件Tab.vue
中使用这个tabs
对象,应该如何将这个对象传递给子组件呢?
List.vue
<template>
<tab></tab>
<div class="col-lg-12">
<table class="table table-bordered table-responsive">
<thead>
<tr>
<th>#</th>
<th>category name</th>
<th>superior belong</th>
<th>sort</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr v-for="cat in categories">
<td>{{ cat.id }}</td>
<td>{{ cat.name }}</td>
<td>{{ cat.parent_id }}</td>
<td>{{ cat.sort_order }}</td>
<td>
<a class="btn btn-primary" v-link="{ path: '/category/edit/' }"><i class="fa fa-edit"> </i>edit</a>
<a class="btn btn-danger" v-link="{ path: '/category/delete/' }"><i class="fa fa-remove"> </i>delete</a>
</td>
</tr>
</tbody>
</table>
<pagination></pagination>
</div>
</template>
<script>
var Pagination = require('../common/Pagination.vue');
var Tab = require('../common/Tab.vue');
export default{
components:{
tab: Tab,
pagination: Pagination
},
data() {
return {
categories: [],
tabs: [
{
'name': 'Foo',
'url': '/foo'
},
{
'name': 'bar',
'url': '/bar'
}],
active: '1'
};
},
ready() {
this.$http.get('/categories').then((response) => {
console.log(response.data);
// success
this.$set('categories', response.data.categories);
this.$set('tabs', response.data.tabs);
this.$set('active', response.data.active);
}, (response) => {
// error
console.log(response);
})
}
}
</script>
Tab.vue
在这里我已经使用了
props
,这样就可以直接使用tabs
了吗?好像不行,求各位大神解答。
<template>
<div class="col-lg-12 gap-bottom">
<ul v-for="tab in tabs" class="nav nav-tabs">
<li role="presentation"><a href="#!{{ tab.url }}">{{ tab.name }}</a></li>
</ul>
</div>
</template>
<script>
export default {
props: [
'tabs',
'active'
],
ready() {
console.log('tab ready');
console.log(this.$data);
console.log(this.tabs);
}
}
</script>
解决方案
给子组件传递数据的方式不太对吧,如果想给子组件传递数据,需要使用v-bind
动态绑定到子组件上,所以
<pagination :tabs="tabs"></pagination>
这样你在子组件的props
中写tabs
才可以
这篇关于javascript - vuejs父组件如何向子组件传值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文