简体   繁体   English

请求后Vue变量值未更新

[英]Vue variable value not updating after a request

Hi everyone I'm having trouble about variable value.大家好,我在变量值方面遇到了麻烦。

Inside the request I'm matching each array values if their is a match then update the match variable to true if their is.在请求中,我匹配每个数组值(如果它们是匹配的),然后将匹配变量更新为 true(如果它们是)。 The problem is that the returned match variable is not updating outside the request after it gets updated inside the request.问题是返回的匹配变量在请求内部更新后没有在请求外部更新。 How am I able to get the updated match value?我怎样才能获得更新的匹配值?

Template模板

<template>
   <div>{{$acl(['post.edit'])}}</div> <!-- always display false -->
</template>

Nuxt plugin Nuxt插件

let match = false
let moduleName = ''
let actionName = ''

Vue.prototype.$acl = ( access ) => {

    let bindAccess = access

    storeRequest.then( done => {
        _.each( bindAccess, ( value, index ) => {
            moduleName = value.split('.')[0]
            actionName = value.split('.')[1]

            /**
             * check if user access modules match with bindAccess module
             */
            if( aclConfig[moduleName] != undefined ) {
                _.each( aclConfig[moduleName], ( actions, index ) => {
                    if(actions.action_name === actionName) {
                        match = true
                        return false
                    }
                })
            }
        })

        console.log(match) //returns true since their is a match
    })

    console.log(match) //always returns false
    return match
}

The promise get resolved after the method returns so match is changed after the method returns, hence you'll always get false.方法返回会解析承诺,因此方法返回后match会发生变化,因此您总是会得到 false。

You should declare a field on the component instance and then change that var from inside the plugin.您应该在组件实例上声明一个字段,然后从插件内部更改该变量。

<template>
 <div>{{$acl(['post.edit']), isAMatch}}</div>
</template>

and in the plugin并在插件中

Vue.prototype.$acl = ( access ) => {

 let bindAccess = access

 storeRequest.then( done => {
    _.each( bindAccess, ( value, index ) => {
        moduleName = value.split('.')[0]
        actionName = value.split('.')[1]

        /**
         * check if user access modules match with bindAccess module
         */
        if( aclConfig[moduleName] != undefined ) {
            _.each( aclConfig[moduleName], ( actions, index ) => {
                if(actions.action_name === actionName) {
                    this.isAMatch = true
                    return false
                }
            })
        }
    })
 })

 // no need to return here
 // return match
}

You must use mixins, like this:您必须使用 mixins,如下所示:

{
  data: () => {
    return { 
       aclConfig
    }
  },
  created() {
    this.aclConfig = this.$store.getAclConfig(); // or maybe using Promise
  },
  methods: {
    checkAccess(roles) {
       let match = false;
       _.each(roles, (role) => {
         const splited = value.split('.');
         const moduleName = splited[0];
         const actionName = splited[1];
         if (this.aclConfig[moduleName]) {
            _.each(this.aclConfig[moduleName], (actions) => {
              if (actions.action_name === actionName) {
                match = true;
                return false;
              }
           });
        }
      });

      return match;
    }
  }
}

And you can use this in template like this:你可以像这样在模板中使用它:

<template>
 <div>{{ checkAccess(['post.edit']) }}</div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM