简体   繁体   English

绑定 ref 在 vue.js 中不起作用?

[英]binding a ref does not work in vue.js?

When I v-bind a element-ref with :ref="testThis" it stops working it seems.当我使用:ref="testThis" v-bind 元素引用时,它似乎停止工作。 Compare this version which works:比较这个有效的版本

<template>
    <div>
        <q-btn round big color='red' @click="IconClick">
        YES
        </q-btn>
        <div>
            <input
                ref="file0"
                multiple
                type="file"
                accept=".gif,.jpg,.jpeg,.png,.bmp,.JPG"
                @change="testMe"
                style='opacity:0'
            >
        </div>
    </div>
</template>

<script>
import { QBtn } from 'quasar-framework'

export default {
  name: 'hello',
    components: {           
         QBtn
    },
    data () {
    return {
      file10: 'file0'
    }
  },
  methods: {
    IconClick () {
      this.$refs['file0'].click()
    },
        testMe () {
            console.log('continue other stuff')
        }
    }
}

</script>

With this one which DOES NOT work:使用这个不起作用:

<template>
    <div>
        <q-btn round big color='red' @click="IconClick">
        YES
        </q-btn>
        <div>
            <input
                :ref="testThis"
                multiple
                type="file"
                accept=".gif,.jpg,.jpeg,.png,.bmp,.JPG"
                @change="testMe"
                style='opacity:0'
            >
        </div>
    </div>
</template>

<script>
import { QBtn } from 'quasar-framework'

export default {
  name: 'hello',
    components: {           
         QBtn
    },
    data () {
    return {
      file10: 'file0'
    }
  },
  methods: {
    IconClick () {
      this.$refs['file0'].click()
    },
        testThis () {
            return 'file0'
        },
        testMe () {
            console.log('continue other stuff')
        }
    }
}

</script>

The first one works.第一个有效。 The second one throws an error:第二个抛出错误:

TypeError: Cannot read property 'click' of undefined
    at VueComponent.IconClick

As I would like to vary the ref based on a list-index (not shown here, but it explains my requirement to have a binded ref) I need the binding.因为我想根据列表索引(此处未显示,但它解释了我需要绑定 ref)来改变 ref,所以我需要绑定。 Why is it not working/ throwing the error?为什么它不起作用/抛出错误?

In the vue docs I find that a ref is non-reactive: "$refs is also non-reactive, therefore you should not attempt to use it in templates for data-binding."在 vue 文档中,我发现ref 是非反应性的:“$refs 也是非反应性的,因此您不应尝试在模板中使用它来进行数据绑定。”

I think that matches my case.我认为这符合我的情况。

My actual problem 'how to reference an item of a v-for list' is NOT easily solved not using a binded ref as vue puts all similar item-refs in an array, BUT it loses (v-for index) order .我的实际问题“如何引用 v-for 列表的项目”并不容易解决,因为 vue 将所有类似的 item-refs 放入数组中,但它失去了 (v-for index) order

I have another rather elaborate single file component which works fine using this piece of code:我有另一个相当精细的单文件组件,使用这段代码可以正常工作:

 :ref="'file' + parentIndex.toString()"

in an input element.在输入元素中。 The only difference from my question example is that parentIndex is a component property.与我的问题示例的唯一区别是parentIndex是一个组件属性。

All in all it currently is kind of confusing as from this it looks like binding ref was allowed in earlier vue version.总而言之,它目前有点令人困惑,因为从这里看起来绑定 ref 在早期的 vue 版本中是允许的。

EDIT: Triggering the method with testThis() does work .编辑:用testThis()触发方法确实有效

If you want to use a method, you will need to use the invocation parentheses in the binding to let Vue know you want it to bind the result of the call and not the function itself.如果你想使用一个方法,你需要在绑定中使用调用括号,让 Vue 知道你希望它绑定调用的结果,而不是函数本身。

             :ref="testThis()"

I think the snippet below works as you expect it to.我认为下面的代码片段可以按您的预期工作。 I use a computed rather than a method.我使用computed而不是方法。

 new Vue({ el: '#app', data() { return { file10: 'file0' } }, computed: { testThis() { return 'file0'; } }, methods: { IconClick() { this.$refs['file0'].click() }, testMe() { console.log('continue other stuff') } } });
 <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script> <div id="app"> <q-btn round big color='red' @click="IconClick"> YES </q-btn> <div> <input :ref="testThis" multiple type="file" accept=".gif,.jpg,.jpeg,.png,.bmp,.JPG" @change="testMe" style='opacity:0'> </div> </div>

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

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