vueJS [Vue warn]:计算属性“title”已在数据中定义

问题描述 投票:1回答:2
<script>
import alertStore from '../stores/alert';
Vue.component('alert', require('vue-strap').alert);

export default {
    data () {
        return {
            show: alertStore.state.show,
            title: alertStore.state.title,
            msg: alertStore.state.msg,
            type: alertStore.state.type
        }
    },
    created () {
    },
    computed: {
        title () {

            return alertStore.state.title;
        },
        msg () {
            return alertStore.state.msg;
        },
        type () {
            return alertStore.state.type;
        },
        show () {
            return alertStore.state.show;
        },
        duration () {
            return alertStore.state.duration;
        }
    },
    methods: {
        dismissAlert: function () {
            this.$store.dispatch('dismissAlert', {title: '...'});
        },
    }
}

命名空间如何在Vue中工作?是否将数据键,计算的返回对象键和所有组件对象键添加到此实例中?

所以,如果我重写这个。我收到一些错误:

[Vue警告]:计算属性“title”已在数据中定义。 [Vue警告]:计算属性“show”已在数据中定义。 [Vue警告]:计算属性“类型”已在数据中定义。 [Vue警告]:计算属性“msg”已在数据中定义。

我该如何解决这个问题。

提前致谢。

vuejs2
2个回答
0
投票
    <style lang="sass" scoped>
    @import '../../sass/_variables.scss';
    .dismiss {
        cursor: pointer;
        position: absolute;
        right: 0;
        top: 0;
        padding: 10px;
        margin: 10px;
    }
    .alert {
        width: 40%;
        border-radius: 0;
        border-width: 5px;
        margin: 10px;
        .row {
            margin: 0;
            .header {
                font-size: 1.25em;
                font-weight: 800;
            }
        }
    }
</style>
<template>
    <div>
        <alert class='card' v-show="show" placement="top" :duration="duration" :type="type">
            <div class="row">
                <div class="header">
                    {{ title }}
                </div>
                <div class='message'>
                    {{ msg }}
                </div>
            </div>
            <div class="dismiss" title="Click to dismiss" @click="dismissAlert">
                <i class="fa fa-times" aria-hidden="true"></i>
            </div>
        </alert>
    </div>
</template>
<script>
    import alertStore from '../stores/alert';
    Vue.component('alert', require('vue-strap').alert);

    export default {
        data () {
            return {
                show: alertStore.state.show,
                title: alertStore.state.title,
                msg: alertStore.state.msg,
                type: alertStore.state.type
            }
        },
        created () {
        },
        computed: {
            title () {
                return alertStore.state.title;
            },
            msg () {
                return alertStore.state.msg;
            },
            type () {
                return alertStore.state.type;
            },
            show () {
                return alertStore.state.show;
            },
            duration () {
                return alertStore.state.duration;
            }
        },
        methods: {
            dismissAlert: function () {
                this.$store.dispatch('dismissAlert', {title: '...'});
            },
        }
    }
</script>

这是我的模板代码


0
投票

Vue将data方法中的所有属性绑定到实例的根。它还对计算属性和方法执行此操作,因此必须使用不同的名称以避免命名冲突。

您发布的代码中的主要问题是您与每个数据属性都存在命名冲突。实际上,因为您使用的是Vuex存储,所以根本不需要数据属性,只需要计算属性。

这也不是使用Vuex商店的最佳方式。一般建议使用mapGetters作为文件here

© www.soinside.com 2019 - 2024. All rights reserved.