账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vuex与computed有时有效,有时无效没有更新页面
    42
    0

    1.两个相同的功能,同样的方法,一个有效,一个无效
    2.无效的代码:

    <Row :gutter="10" style="margin-top: 10px" v-for="(col, index) in categoryList" :key="index">
        <Col span="4" v-for="(item, index) in col" :key="index">
            <category-info :info="item"></category-info>
        </Col>
    </Row>
    mounted() {
        this.getCategoryList()
    },
    computed: {
        categoryList() {
            let list = _.chunk(this.$store.state.category.categoryList, 6)
            return list
        },
        categoryTotal() {
            return this.$store.state.category.categoryTotal
        }
    },
    methods: {
        getCategoryList() {
            this.$store.dispatch('getCategoryList')
        }
    }

    3.categoryInfo

    props: {
        info: Object,
    },

    4.store

    const category = {
        state: {
            categoryList: [],
            categoryTotal: 0,
        },
        mutations: {
            GETCATEGORYLIST (state, data) {
                state.categoryTotal = data.data.length
                let categoryList = data.data
                state.categoryList = categoryList
            },
            DELETECATEGORY (state, typeId) {
                state.categoryTotal--
                state.categoryList = _.remove(state.categoryList, v => {
                    return v.typeId != typeId
                })
            }
        },
        actions: {
          // category
            getCategoryList (context) {
                Util.ajax
                .get('/type/all')
                .then(res => {
                    context.commit("GETCATEGORYLIST",res.data)
                })
            },
            deleteCategory (context, typeId) {
                Util.ajax
                .delete(`/type/delete?typeIds=${typeId}`)
                .then(res => {
                    if(res.data.status == 0) {
                        context.commit("DELETECATEGORY", typeId)
                      }
                })
            },
        }
    }
    
    export default category

    刷新页面->mounted->重新获取数据,这个时候 store 是有数据的变化的,但是页面是空白

    同样的方式另外一个产品列表就是有效的,很奇怪,是因为computed的对象是数组的原因吗?还是写法上有什么问题

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 三年如梦 普通会员 1楼

      Vuex和computed是Vue.js中两种重要的数据管理和计算方式。在使用它们时,需要注意以下几点以确保它们的正确使用:

      1. 使用 computed 函数进行计算:在计算属性中,使用 computed 函数可以轻松地处理计算后的数据。例如,如果你有一个数组对象,你可以使用 computed 函数来根据指定的条件过滤数据:

      javascript export default { data() { return { items: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, ], filteredItems: computed(function() { return this.items.filter(item => item.id === 1); }) }; } };

      1. 使用 Vuex 提供的 store:Vuex 提供了一个 store 来管理组件的数据。通过使用 Vuex,你可以在一个全局的地方处理组件的数据,而不需要在每个组件中手动计算和更新数据。例如:

      ```javascript import Vue from 'vue'; import Vuex from 'vuex';

      Vue.use(Vuex);

      export default new Vuex.Store({ state: { items: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, ], filteredItems: computed(function() { return this.items.filter(item => item.id === 1); }) }, mutations: { updateItems(state, items) { state.items = items; }, }, actions: { updateItems({ commit }) { commit('updateItems', items); }, }, }); ```

      1. 注意缓存和同步:如果你的计算结果在存储中,那么你需要确保这些结果在其他组件中也是有效的。例如,如果你的计算结果是实时更新的,那么你应该使用 Vuex 的 store 来同步数据,而不是在每个组件中手动更新数据。同时,你也需要注意缓存的问题,因为如果你的计算结果在长时间内没有更新,那么其他组件可能无法正确地使用这些数据。

      总的来说,Vuex 和 computed 是 Vue.js 中非常有用的工具,但它们的效果可能取决于你的具体需求和使用场景。在使用它们时,你应该充分考虑这些因素,并根据你的具体情况做出最佳选择。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部