如何将对象数组作为道具传递给组件,然后将数组的成员作为道具传递给嵌套组件?
How can I pass down an array of objects as a prop to a component and then pass down the array's members as props to nested components?
在这个 post 编辑的底部,我解决了我想要完成的事情以及几乎有效的事情
以下是我的应用程序,在应用程序上方定义了 <oo-upload>
和 <oo-uploads>
组件。本质上 <oo-uploads>
显示 table 个 <oo-upload>
组件来为我的应用构建文件上传插件。 uploads
变量是所有上传的列表,upload
定义每个单独的上传。
<body>
<script type="x/template" id="oo-upload-template">
<td>@{{ upload.file.name }}</td>
<td>@{{ upload.file.size }}</td>
<td>
<div class="ui indicating progress floated" v-progress="upload.progress">
<div class="bar"><div class="progress"></div></div>
</div>
</td>
<td>
<button class="ui primary button" v-on:click="upload" v-if="status < 1">Upload</button>
<button class="ui red button" v-on:click="destroy" v-if="status == 2">Delete</button>
</td>
</script>
<script type="x/template" id="oo-uploads-template">
<table class="ui very basic table">
<thead>
<tr>
<th class="two wide">Filename</th>
<th class="two wide">Filesize</th>
<th class="ten wide">Status</th>
<th class="two wide">Actions</th>
</tr>
</thead>
<tbody>
<tr v-show="uploads.length==0">
<td colspan="4" class="ui center aligned">No files added!</td>
</tr>
<tr v-for="upload in uploads">
<oo-upload :upload="upload"></oo-upload>
</tr>
</tbody>
<tfoot class="full-width">
<tr>
<th colspan="4">
<div class="ui right floated small green labeled icon button" v-on:click="uploadDialog">
<i class="plus icon"></i> Upload File
<input type="file" style="display:none;" v-el:uploader v-on:change="addFiles" multiple>
</div>
</th>
</tr>
</tfoot>
</table>
</script>
<div id="app">
<div class="ui container">
<oo-uploads :uploads="uploads"></oo-uploads>
</div>
</div>
<script type="text/javascript" src="js/app.js"></script>
</body>
问题是 objUpload
对象没有传递给 <oo-upload>
组件的每个实例。相反,Vue 调试器说组件正在传递一个函数,而不是一个对象。 <oo-uploads>
接收 uploads
作为道具没有任何问题。
var Vue = require('vue'),
VueRouter = require('vue-router'),
VueResource = require('vue-resource'),
Vuex = require('vuex'),
VueValidator = require('vue-validator');
/*
PLUGINS
*/
Vue.use(VueResource);
/*
CUSTOM DIRECTIVES
*/
Vue.directive('progress', {
bind: function () {
$(this.el).progress();
},
update: function (value) {
$(this.el).progress('set percent', value);
}
});
/*
OBJECTS
*/
function objUpload (file) {
this.progress = 0;
this.file = file;
this.status = 0;
}
/*
COMPONENTS
*/
Vue.component('oo-upload', {
props: ['upload'],
template: '#oo-upload-template',
methods: {
upload: function () {
this.upload.status = 1;
this.$http.post('/upload', this.upload.file, { progress: function (pe) {
this.progress = Math.floor(pe.loaded/pe.total * 100);
}}).then(function (result) {
this.upload.status = 2;
}, function (result) {
this.upload.status = -1;
})
},
destroy: function () {
}
}
});
Vue.component('oo-uploads', {
props: ['uploads'],
template: '#oo-uploads-template',
methods: {
uploadDialog: function () {
$(this.$els.uploader).click();
},
addFiles: function () {
var uploader = this.$els.uploader;
for (var i = 0; i < uploader.files.length; i++) {
var file = uploader.files[i];
this.uploads.push(new objUpload(file));
}
}
}
})
/*
CONSTANTS
*/
Vue.http.headers.common['X-CSRF-TOKEN'] = $('meta[name="_token"]').attr('content');
/*
INSTANCE
*/
var vm = new Vue({
el: '#app',
data: {
uploads: [],
},
});
编辑:如果我将 uploads
数组直接传递给 <oo-uploads>
中的单个 <oo-upload>
实例,它会很好地传递整个数组,但由于某种原因它赢了't 遍历数组并仅传递 objUpload
个对象。
EDIT2:基本上我想做的是将我传递的数据范围限制为该组件所需的数据。我希望上传组件仅对分配给它的上传起作用。我认识到我的做法可能很差或者我的实施可能无法实现,我只需要一个关于如何完成类似事情的指导。
It is therefore recommended to always have a single root-level, plain element in templates.
尝试用 tr
包裹 oo-upload-template
并更改
<tr v-for="upload in uploads">
<oo-upload :upload="upload"></oo-upload>
</tr>
到
<tr is="oo-upload" v-for="upload in uploads" :upload="upload"></tr>
在这个 post 编辑的底部,我解决了我想要完成的事情以及几乎有效的事情
以下是我的应用程序,在应用程序上方定义了 <oo-upload>
和 <oo-uploads>
组件。本质上 <oo-uploads>
显示 table 个 <oo-upload>
组件来为我的应用构建文件上传插件。 uploads
变量是所有上传的列表,upload
定义每个单独的上传。
<body>
<script type="x/template" id="oo-upload-template">
<td>@{{ upload.file.name }}</td>
<td>@{{ upload.file.size }}</td>
<td>
<div class="ui indicating progress floated" v-progress="upload.progress">
<div class="bar"><div class="progress"></div></div>
</div>
</td>
<td>
<button class="ui primary button" v-on:click="upload" v-if="status < 1">Upload</button>
<button class="ui red button" v-on:click="destroy" v-if="status == 2">Delete</button>
</td>
</script>
<script type="x/template" id="oo-uploads-template">
<table class="ui very basic table">
<thead>
<tr>
<th class="two wide">Filename</th>
<th class="two wide">Filesize</th>
<th class="ten wide">Status</th>
<th class="two wide">Actions</th>
</tr>
</thead>
<tbody>
<tr v-show="uploads.length==0">
<td colspan="4" class="ui center aligned">No files added!</td>
</tr>
<tr v-for="upload in uploads">
<oo-upload :upload="upload"></oo-upload>
</tr>
</tbody>
<tfoot class="full-width">
<tr>
<th colspan="4">
<div class="ui right floated small green labeled icon button" v-on:click="uploadDialog">
<i class="plus icon"></i> Upload File
<input type="file" style="display:none;" v-el:uploader v-on:change="addFiles" multiple>
</div>
</th>
</tr>
</tfoot>
</table>
</script>
<div id="app">
<div class="ui container">
<oo-uploads :uploads="uploads"></oo-uploads>
</div>
</div>
<script type="text/javascript" src="js/app.js"></script>
</body>
问题是 objUpload
对象没有传递给 <oo-upload>
组件的每个实例。相反,Vue 调试器说组件正在传递一个函数,而不是一个对象。 <oo-uploads>
接收 uploads
作为道具没有任何问题。
var Vue = require('vue'),
VueRouter = require('vue-router'),
VueResource = require('vue-resource'),
Vuex = require('vuex'),
VueValidator = require('vue-validator');
/*
PLUGINS
*/
Vue.use(VueResource);
/*
CUSTOM DIRECTIVES
*/
Vue.directive('progress', {
bind: function () {
$(this.el).progress();
},
update: function (value) {
$(this.el).progress('set percent', value);
}
});
/*
OBJECTS
*/
function objUpload (file) {
this.progress = 0;
this.file = file;
this.status = 0;
}
/*
COMPONENTS
*/
Vue.component('oo-upload', {
props: ['upload'],
template: '#oo-upload-template',
methods: {
upload: function () {
this.upload.status = 1;
this.$http.post('/upload', this.upload.file, { progress: function (pe) {
this.progress = Math.floor(pe.loaded/pe.total * 100);
}}).then(function (result) {
this.upload.status = 2;
}, function (result) {
this.upload.status = -1;
})
},
destroy: function () {
}
}
});
Vue.component('oo-uploads', {
props: ['uploads'],
template: '#oo-uploads-template',
methods: {
uploadDialog: function () {
$(this.$els.uploader).click();
},
addFiles: function () {
var uploader = this.$els.uploader;
for (var i = 0; i < uploader.files.length; i++) {
var file = uploader.files[i];
this.uploads.push(new objUpload(file));
}
}
}
})
/*
CONSTANTS
*/
Vue.http.headers.common['X-CSRF-TOKEN'] = $('meta[name="_token"]').attr('content');
/*
INSTANCE
*/
var vm = new Vue({
el: '#app',
data: {
uploads: [],
},
});
编辑:如果我将 uploads
数组直接传递给 <oo-uploads>
中的单个 <oo-upload>
实例,它会很好地传递整个数组,但由于某种原因它赢了't 遍历数组并仅传递 objUpload
个对象。
EDIT2:基本上我想做的是将我传递的数据范围限制为该组件所需的数据。我希望上传组件仅对分配给它的上传起作用。我认识到我的做法可能很差或者我的实施可能无法实现,我只需要一个关于如何完成类似事情的指导。
It is therefore recommended to always have a single root-level, plain element in templates.
尝试用 tr
包裹 oo-upload-template
并更改
<tr v-for="upload in uploads">
<oo-upload :upload="upload"></oo-upload>
</tr>
到
<tr is="oo-upload" v-for="upload in uploads" :upload="upload"></tr>