Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding style in $refs?

Can't add style in $refs attribute. Cannot set property 'cssText' of undefined. Is this possible? Can't find any similar to this

this.$refs['ticketCode_'+this.resoTrans_id].style.cssText =
                "background-color:#66BB6A !important; border:1px solid #66BB6A !important; color:#fff !important;";

Printing out without .style seems work fine console.log(this.$refs['ticketCode_'+this.resoTrans_id])

VueComponent {_uid: 493, _isVue: true, $options:
like image 405
BGTabulation BGTabulate Avatar asked Mar 30 '18 10:03

BGTabulation BGTabulate


People also ask

How do you use refs in react?

Working with refs in React. Refs make it possible to access DOM nodes directly within React. This comes in handy in situations where, just as one example, you want to change the child of a component. Let’s say you want to change the value of an <input> element, but without using props or re-rendering the whole component.

How do you create a ref In HTML?

The ref is created using a callback like we did in the first example of this section. The key lies in how we access the DOM of the input element in the Input component from the App component. If you look closely, we access it using this.inputElement.

How does the styleref field affect which text is inserted?

* Where you insert the STYLEREF field determines the direction that Microsoft Word searches in for the style and thus can affect which text is inserted. * When you insert the field in: Document text Microsoft Word searches for the closest text preceding the STYLEREF field. If the style isn't found, Word searches for the closest following text.

When to use the ref attribute on a custom class?

When the ref attribute is used on a custom class component, the ref object receives the mounted instance of the component as its current. You may not use the ref attribute on function components because they don’t have instances. The examples below demonstrate the differences. Adding a Ref to a DOM Element


1 Answers

You're not supposed to use cssText but instead use the JavaScript API to set styles (which you can do on $refs, too):

let $ref = this.$refs['ticketCode_' + this.resoTrans_id]

$ref.style.backgroundColor = '#66bb6a';
$ref.style.border = '1px solid #66bb6a';
...

Even better is to utilize Vue's power for that directly in your template:

<your-element ref="'ticketCode_' + resoTrans_id" :style="{ backgroundColor: '#66bb6a', border: '1px solid #66bb6a' /* ... */ }" />
like image 149
Benjamin Schmidt Avatar answered Oct 17 '22 08:10

Benjamin Schmidt