【问题标题】:Computed property on child component props子组件道具的计算属性
【发布时间】:2018-02-07 14:35:51
【问题描述】:

我有这个设置,其中我有子组件道具,其中有日期时间格式,我想在我的表格中将其更改为更易于阅读的格式,所以我使用 moment 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>

这是我的数据查看器文件中的内容

<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 中更改?

【问题讨论】:

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


    【解决方案1】:

    好像用filter will work here:

    而不是使用计算道具:

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

    代替 {{publishDate}}

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

    【讨论】:

    • 哇,谢谢伙计,它的工作原理很神奇......我完全忘记了过滤器属性哈哈哈
    猜你喜欢
    • 2021-12-20
    • 2018-01-22
    • 1970-01-01
    • 2017-11-23
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2021-03-04
    相关资源
    最近更新 更多