Javascript& VueJS变量访问 [英] Javascript & VueJS variable access

查看:62
本文介绍了Javascript& VueJS变量访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 success Dropzone事件回调中访问VueJS变量。
所有代码都没问题,DropzoneJS& VueJS可以很好地协同工作,但是在成功回调中无法访问我的变量 photos

I'd like to access a VueJS variable from the success Dropzone event callback. All the code is OK, DropzoneJS & VueJS work fine together, but my variable photos is not accessible in the success callback.

这是一个简单的实现我的剧本:

Here is a simple implementation of my script :

<script>
    import Dropzone from 'dropzone';

    export default {  
        data() {
            return {
                photos: []
            };
        },

        ready() {
            Dropzone.autoDiscover = false;

            new Dropzone('form#upload-form', {
                url: '...',
                success: function(file, response) {

                    this.photos = response.data.photos;

                    // this.photos is not accessible here

                }
            });
        }
    }
</script>

是否有以这种方式访问​​VueJS组件变量的最佳做法?
谢谢

Is there a best practice to access VueJS components variables this way? Thanks

推荐答案

你目前的做法,你可能会遇到<$ c的范围问题$ c>此参考。

The way you're doing it currently, you may have a scoping issue with the this reference.

我建议在<$之外重新分配 c $ c> Dropzone 实例化并使用它就像这样......

I suggest reassigning this outside of the Dropzone instantiation and using that like so...

ready() {
    Dropzone.autoDiscover = false;

    const self = this;

    new Dropzone('form#upload-form', {
        url: '...',
        success: function(file, response) {

            self.photos = response.data.photos;

            // this.photos is not accessible here

        }
    });
}

这篇关于Javascript&amp; VueJS变量访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆