Vue - Problem when passing props with a colon into the component - vue.js

I want to pass TailwindCSS pseudo-classes to a child component. However, I get an error message (Parsing error. Unexpected token :...).
Here is my code:
<cartImage :images="p.images" :classNames="hover:grow hover:shadow-lg" />

It seems you just need to pass a static value to the classNames prop. So you only need to remove the colon like this:
<cartImage :images="p.images" classNames="hover:grow hover:shadow-lg" />

When you do this :
:classNames="hover:grow hover:shadow-lg"
it will think of hover:grow hover:shadow-lg as a valid statement or variable - which will fail at this point.
If you want to pass a String you have 2 Options :
Either with a static prop
classNames="hover:grow hover:shadow-lg"
Or you wrap the input inside 2 single quotes '...' :
:classNames="'hover:grow hover:shadow-lg'"

Related

How to use v-text-area rules option?

I am trying to set rules to control buttons that are inside of v-text-area on Vue2/Vuetify. How can I do this?
I tried several things, please do not judge me i am beginner of coding concept
In order to use Vuetify validation rules, you need to wrap all elements on which you want to perform validation in a <v-form> element.
On your input components, you need to provide an array to the rules prop with the names of functions you define which perform validation.
The functions which validate take the value as an input and return true if the input is valid and false or a failure string if the input is invalid.
An example of such a function defined in the methods section would be:
isNumber(input) {
return /[0-9]+/g.test(input) || "input must be a number";
}
Passing it to your v-text-area would look like this:
<v-text-area :rules="[isNumber]" />
More info is available in the #rules section of Vueitfy's Form docs.

Vue js - is that right to put all my code in watch?

I Have component A and Component B
in component A i have an API call.
when i passing info to my component b:
<B :structuresMetaData="structureTree"></B>
Inside mounted the variable structuresMetaData the length is 0
and inside the watch the length is 1.
my issue that mounted appear before the watch.
is it would be right to put all my code in watch ? if not what is the solution ?
It looks like structureTree is being changed after <B> is created/mounted.
You have two options depending on your use case:
The first option is to defer the creation of <B> until your data is loaded. This way you don't need <B> to watch for changes to the prop and the prop will be fully populated in the created/mounted hook. This is the simpler approach.
Assuming structureTree is initially an empty array:
<B
v-if="structureTree.length > 0"
:structuresMetaData="structureTree"
/>
created() {
// this.structuresMetaData is a non-empty array here
}
I usually initialize my data with null so that I can distinguish between "not loaded" and "loaded" (where "loaded" can actually be an empty array if the data I fetched from the API was empty).
The second way is using a watcher in <B> to react to changes to the prop. You will need to do this method if the bound prop might change multiple times and you need <B> to update in some way to that change.
watch: {
structuresMetaData(value) {
// React to the change, load secondary data, etc
}
}
What exactly are you doing in <B> that requires a watcher? It's best to avoid explicit watchers if you can avoid them because it usually means you are copying data around and it gets messy when you don't have a single source of truth. Use pure computed properties whenever you can if you just want to transform the data in some way.

Bind multiple classes to a single variable

While using Tailwind with utility-first approach to css, I often find the need to bind multiple classes to a single variable.
For instance, to style an input form, I need to add border-red, color-red, etc if there is an error.
Is there a nice and elegant way to express this in Vue instead of writing v-bind:class="{ 'border-red': error, 'text-red': error }?
You can combine both classes into the same property:
:class="{ 'border-red text-red': error }"
Another easy solution:
:class="error && 'border-red text-red'"
or for if, else
:class="error ? 'border-red text-red' : 'border-green'"
You also can concatenate strings to classnames:
:class="'border-'+borderColor"

how to solve vue short if in v-model?

I need to do a shortif in a v-model, but eslint gives the folowing problem:
[vue/valid-v-model] 'v-model' directives require the attribute value
which is valid as LHS.eslint-plugin-vue
so the code works. but its not the way it needs to work.
this is the code i have now
<v-text-field
v-show="field.type == 'String'"
v-model="_isMultiple(content_data[tabindex]) && subitem != null ? content_data[tabindex][subitem][field.name]
: content_data[tabindex][field.name]"
:label="field.name"
:counter="field.counter"
max-width="100px"
/>
So this code needs a little explanation to it.
I try to build this as an dynamic module. If I get an array back from my json response it needs to v-model the subitem. If I get an object back from the response it just needs to v-model that object.
The data (content_data[tabindex]) + field do i get from a v-for loop and other loops in my vue html
so I think its not an option to do a computed prop because
I can't get in the right data.
_isMultiple function code:
_isMultiple(content_data) {
return Array.isArray(content_data)
}
any solution for this?
Maybe you should not use v-model, but build it on your own with value binding and event listening.
v-model is only a shorthand: https://v2.vuejs.org/v2/guide/forms.html.
By implementing it on your own you can use a method to set the values and a computed property to get it.

Vue js multiselect - having custom suggestion text

I am using vue multiselect plugin in my Vue v.1x project. I am wondering how can I customise suggestion text like Press enter to select or Press enter to remove, when hovering over options?
You can see the example in the fiddle. I have tried with setting the :selectLabel="Select" but that didn't work.
When sending props you need to use 'kebab-case'. So if the prop looks like selectLabel in the child, it should be pass like
:select-label="value"
Also, when sending the variable make sure to either to double quotes to send as a string, since the ':' before the prop tries to evaluate a variable.
Ex. The label should be 'Select'
:select-label="'Select'"
Ex. 2. The label should use a variable 'xyz' defined in the component
:select-label="xyz"