vue.js - vue 计算 : add new computed value from created lifecycle hook

标签 vue.js vue-component

我是vue初学者

我正在尝试使用 name 推送计算数据,该名称来自创建实例后出现的 vuex

如何将新的计算属性推送到 created() Hook 中的实例??

这是代码

computed: {
            // 3 - i want to extract the object properties here computed 
            // that is easy bu using spread operator ...
            // Problem : vue intialize computed before the created() hook
            // so the spreed work befor passing the filling the object
            ...this.mapGettersObj
        },

        created(){
            // 1- i can access only this line after creating the object
            this.stocks = this.$store.state
            let arr=[]
            for (let stock in this.stocks){
                arr.push(stock+'Getter')
            }
            // 2 - mapGetters returns an object
            this.mapGettersObj=mapGetters(arr)
        }

如果我可以在创建后创建新的计算值就可以解决问题

最佳答案

你可以在 beforeCreate 钩子(Hook)中这样做。

beforeCreate: function() {
  this.$options.computed = {
     demo2: function() {
       return this.demo
     }
  }
}

关于vue.js - vue 计算 : add new computed value from created lifecycle hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782086/

相关文章:

javascript - 在 Vue js 的 app.js 中 window.app=new Vue({}) 而不是 const app = new Vue({}) 有什么区别?

vue.js - 尝试在 v-for 中对数组进行排序时出现无限循环

javascript - Vuejs 在子组件中提交带有输入的表单

javascript - 在组件外使用 VueI18n 的问题

javascript - 在 Vue2Leaflet 中扩展 LMarker

laravel - 如何在 vue 组件上使用 axios 传递上传的文件和传递参数?

javascript - Vue.js 组件调用 data 属性内的方法

vue.js - 从 vuex 和 Vue-native-websocket 插件接收 WebSockets 数据

vue.js - 如何制作可点击的带有悬停效果的q卡?

javascript - Vue.js 父子组件动态双向数据绑定(bind)