Vue公司2 - 在一个模板正常工作的道具,而不是在另一个
问题描述:
正常工作,我有以下Vue公司的主要情况:Vue公司2 - 在一个模板正常工作的道具,而不是在另一个
let App = new Vue({
el: '#app-container',
data: {
countries: []
},
created() {
this.getCountries()
},
methods: {
getCountries() {
let self = this;
axios.get('/admin/countries')
.then(function (response) {
self.countries = response.data;
})
.catch(function (error) {
console.log(error);
});
},
filterCountries(event) {
let name = event.target.value;
let self = this;
if(name.length > 2) {
axios.get('/country/search', {
params: {
name: name
}
})
.then(function (response) {
self.countries = response.data;
})
.catch(function (error) {
console.log(error);
});
}
if((event.keyCode === 8 && name.length == 2) || !name.length){
this.getCountries();
}
},
updateCountry(event) {
let parent = event.target.closest('.parent');
let countryName = parent.getElementsByClassName('country-name')[0].value;
let countryOrder = parent.getElementsByClassName('country-order')[0].value;
axios.post('/country/insert', {
countryName: countryName,
countryOrder: countryOrder
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
})
我有这个模板这是工作:
Vue.component('country-list', {
template: `
<tbody>
<tr is="country" v-for="country in countries.data">
<td>
<input
type="text"
name="name"
class="form-control country-name"
:value="country.name"
>
</td>
<td>{{country.show? 'Yes' : 'No'}}</td>
<td>
<input
type="text"
name="order"
class="form-control country-order"
:value="country.order"
>
</td>
<td>
<button class="btn btn-primary">{{ country.show ? "Hide" : "Show" }}</button>
<button class="btn btn-success"
@click="updateCountry"
:data-id="country.id">Update</button>
</td>
</tr>
</tbody>
`,
props: ['countries'],
methods: {
updateCountry(event) {
let countryID = event.target.dataset.id;
let parent = event.target.closest('.parent');
let countryName = parent.getElementsByClassName('country-name')[0].value;
let countryOrder = parent.getElementsByClassName('country-order')[0].value;
axios.post('/country/insert', {
id: countryID,
name: countryName,
order: countryOrder
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
});
Vue.component('country', {
template: `<tr class=parent><slot></slot></tr>`
});
我试图使用相同的道具(国家),但我没有得到任何东西(在这种情况下,我有以下错误:
vendor.js:753 [Vue warn]: Error in render function: "TypeError: Cannot read property 'current_page' of undefined"
found in
---> <PaginationList>
<Root>
):
Vue.component('pagination-list', {
template: `
<tfoot>
<tr>
<td>{{countries.current_page}}</td>
</tr>
</tfoot>
`,
props: ['countries']
});
HTML代码中使用的模板:
<table class="table table-striped table-hover">
<thead>
<tr>
<td>
<input
class="form-control"
type="text"
id="country-filter"
@keyup="filterCountries"
placeholder="Filter countries by name">
</td>
</tr>
<tr>
<td>Country Name</td>
<td>Visible</td>
<td>Order</td>
<td>Actions</td>
</tr>
</thead>
<tbody is="country-list" :countries="countries"></tbody>
<tfoot is="pagination-list"></tfoot>
</table>
的console.log(self.countries)数据:
Object {…}
current_page:1
data:Array(20)
from:1
last_page:13
next_page_url:"http://smuvajse.app/admin/countries?page=2"
path:"http://smuvajse.app/admin/countries"
per_page:20
prev_page_url:null
to:20
total:249
__ob__:Observer
get current_page:function reactiveGetter()
set current_page:function reactiveSetter(newVal)
get data:function reactiveGetter()
set data:function reactiveSetter(newVal)
get from:function reactiveGetter()
set from:function reactiveSetter(newVal)
get last_page:function reactiveGetter()
set last_page:function reactiveSetter(newVal)
get next_page_url:function reactiveGetter()
set next_page_url:function reactiveSetter(newVal)
get path:function reactiveGetter()
set path:function reactiveSetter(newVal)
get per_page:function reactiveGetter()
set per_page:function reactiveSetter(newVal)
get prev_page_url:function reactiveGetter()
set prev_page_url:function reactiveSetter(newVal)
get to:function reactiveGetter()
set to:function reactiveSetter(newVal)
get total:function reactiveGetter()
set total:function reactiveSetter(newVal)
__proto__:Object
答
你不及格通过您的模板道具:
<tfoot is="pagination-list"></tfoot>
应该是:
<tfoot is="pagination-list" :countries="countries"></tfoot>
+0
这是行得通的。我想我会坚持使用JQuery:D。非常感谢帮助我:) – Sasha
在你的第二个模板中,您需要检查,如果你的属性是否存在使用'v型if',因为当元件安装它不存在。 – wostex
当** getCountries **被调用(创建时)时,该属性应该会自动更新,就像在第一个模板中一样,或者我缺少一些东西? – Sasha
我们无法看到安装了“分页列表”的模板。它是主要应用程序的孩子吗?你正在传递道具。另外,我很好奇,你正在将'countries'视为从ajax调用中检索的简单数组,但在脚注中指的是'countries.current_page'。你的回应数据是什么样的? –