子组件道具

时间:2017-08-30 02:22:03

标签: vue.js vuejs2 vue-component vue-router

我正在进行此设置,其中包含日期时间格式的子组件道具,我想在表格中将其更改为更易读的格式,因此我使用时刻js来更改格式并执行这些操作如果我使用计算属性,任务将更有意义。在我的index.vue

中就像这样
<div class="page-container">
    <div class="page-content">
        <div class="content-wrapper">
            <data-viewer :source="source" :thead="thead">
                <template scope="props">
                    <tr>
                        <td>{{props.item.name}}</td>
                        <td>{{props.item.creator}}</td>
                        <td>
                            <i class="icon-checkmark5" v-if="props.item.publish === '0'"></i>
                            <i class="icon-cancel-circle2" v-else></i> 
                            {{props.item.publish}} //for testing purpose to see returned value
                        </td>
                        <td>{{publishDate}}</td> //this is where i put my computed to change created_at format
                    </tr>
                </template>
            </data-viewer>
        </div>
    </div>
</div>

<script type="text/javascript">
import DataViewer from '../../components/dataviewer.vue'
import moment from 'moment'

export default{
    components:{
        DataViewer
    },
    data(){
        return{
            source: '/api/article',
            thead: [
                {title: 'Name', key: 'name', sort: true},
                {title: 'Creator', key: 'creator_id', sort: true},
                {title: 'Publish', key: 'publish', sort: true},
                {title: 'Created', key: 'created_at', sort: true}
            ],
        }
    },
    computed: {
        publishDate: function(){
           return moment(props.item.created_at).format('YYYY-MM-DD')
        }
    }
}
</script>

这是我的dataviewer文件中的内容

<template>
  <table class="table">
    <thead class="bg-primary">
        <tr>
            <th v-for="item in thead">
                <span>{{item.title}}</span>
            </th>
        </tr>
    </thead>
    <tbody>
        <slot v-for="item in model.data" :item="item"></slot>
    </tbody>
</table>
</template>

<script>
    import Vue from 'vue'
    import axios from 'axios'

    export default {
        props: ['source', 'thead'],
        data() {
            return {
                model: {
                    data: []
                },
            }
        },
        beforeMount() {
            this.fetchData()
        },
        methods: {
            fetchData() {
                var vm = this
                axios.get(this.source)
                    .then(function(response) {
                        Vue.set(vm.$data, 'model', response.data.model)

                    })
                    .catch(function(error) {
                        console.log(error)
                    })
            }
        }
    }
</script>

但它不起作用,找不到props.item.created_at,那么我如何更改created_at或任何其他属性项以从我的index.vue更改?

1 个答案:

答案 0 :(得分:3)

似乎使用filter will work here

而不是使用计算道具:

filters: {
    publishDate: function(value){
       return moment(value).format('YYYY-MM-DD')
    }
}

取代{{publishDate}}

<td>{{props.item.created_at | publishedDate }}</td>