简体   繁体   English

如何通过 $refs 向 Vue 组件添加类

[英]How to add class to Vue component via $refs

I need to add class name to some Vue components using their ref names.我需要使用它们的引用名称将类名添加到一些 Vue 组件。 The ref names are defined in a config file. ref 名称在配置文件中定义。 I would like to do it dynamically, to avoid adding class manually on each Vue component.我想动态地做,以避免在每个 Vue 组件上手动添加类。

I have tried to find each component using $refs and if found, add the class name to the element's class list.我尝试使用 $refs 查找每个组件,如果找到,则将类名添加到元素的类列表中。 The class is added, but it is removed as soon as user interaction begins (eg the component is clicked, receives new value etc.)该类已添加,但在用户交互开始后立即删除(例如,单击组件、接收新值等)

Here is some sample code I've tried:这是我尝试过的一些示例代码:

beforeCreate() {
    let requiredFields = config.requiredFields
    this.$nextTick(() => {
        requiredFields.forEach(field => {
            if(this.$refs[field]) {      
                this.$refs[field].$el.classList.add('my-class')
            }
        })
    })
}

你可以使用这个:


this.$refs[field].$el.classList.value = this.$refs[field].$el.classList.value + 'my-class'

the only thing that you need to make sure of is that your config.requiredFields must include the ref name as a string and nothing more or less ... you can achieve that with :您需要确保的唯一一件事是您的config.requiredFields必须将 ref 名称作为string包含在内,仅此而已……您可以通过以下方式实现:

   //for each ref you have 
    for (let ref in this.$refs) {
        config.requiredFields.push(ref)
     }
   // so config.requiredFields will look like this : ['one','two]

here is an example of a working sample :这是一个工作示例的示例:

 Vue.config.devtools = false; Vue.config.productionTip = false; Vue.component('one', { template: '<p>component number one</p>' }) Vue.component('two', { template: '<p>component number two</p>' }) new Vue({ el: "#app", beforeCreate() { let requiredFields = ['one','two'] // config.requiredFields should be like this this.$nextTick(() => { requiredFields.forEach(field => { if(this.$refs[field]) { this.$refs[field].$el.classList.add('my-class') } }) }) } })
 .my-class { color : red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <one ref="one" ></one> <two ref="two" ></two> </div>

I know this question was posted ages ago, but I was playing around with something similar and came across a much easier way to add a class to $refs.我知道这个问题很久以前就已经发布了,但是我正在玩类似的东西,并遇到了一种将类添加到 $refs 的更简单的方法。

When we reference this.$refs['some-ref'].$el.classList it becomes a DOMTokenList which has a bunch of methods and properties you can access.当我们引用this.$refs['some-ref'].$el.classList它变成了一个DOMTokenList ,它有一堆你可以访问的方法和属性。

In this instance, to add a class it is as simple as在这种情况下,添加一个类很简单

this.$refs['some-ref'].$el.classList.add('some-class')

You've to make sure classList.value is an array.您必须确保 classList.value 是一个数组。 By default its a string.默认情况下它是一个字符串。

methods: {
    onClick(ref) {
      const activeClass = 'active-submenu'
      if (!this.$refs[ref].classList.length) {
        this.$refs[ref].classList.value = [activeClass]
      } else {
        this.$refs[ref].classList.value = ''
      }
    },
  },

this post helped me tremendously.这篇文章对我帮助很大。 I needed to target an element within a v-for loop and I ended up writing a little method for it (i'm using Quasar/Vue).我需要在 v-for 循环中定位一个元素,我最终为它编写了一个小方法(我正在使用 Quasar/Vue)。 hopefully this will save someone else some time.希望这会为其他人节省一些时间。

addStyleToRef: function(referEl, indexp, classToAdd) {
            //will add a class to a $ref element (even within a v-for loop) 
            //supply $ref name (referEl - txt), index within list (indexp - int) & css class name (classToAdd txt)
            if ( this.$refs[referEl][indexp].$el.classList.value.includes(classToAdd) ){
                console.log('class already added')
            } else {
                this.$refs[referEl][indexp].$el.classList.value = this.$refs[referEl][indexp].$el.classList.value + ' ' + classToAdd
            }  
        }

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

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