Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js local template variables

I have a needed to create a local variable in my Vue template to shorten references to an otherwise long object $v.form.text, as shown in the example below:

<input 
    :class="{ error: !$v.form.text.required }"
    v-model.trim="$v.form.text.$model" 
/>

I would like to be able to access the $v.form.text nested object in my template as simply text.

<input 
    :class="{ error: !text.required }"
    v-model.trim="text.$model" 
/>

There is usually a lot more code than in the example above, justifying creating a temporary variable, but the problem is the same as shown.

Note: I have already solved this, please see answer below, and simply want to document the solution for others who might be searching for the same.

like image 770
Paul Bastowski Avatar asked Dec 11 '18 20:12

Paul Bastowski


People also ask

What is $t in VueJS?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue.

Is Vue js better than react?

VueJS is two-way binding; whereas ReactJS is one-way binding and that's why VueJs uses more computer resources than ReactJS. Moreover, looking at the learning curve, Vue is easier than React and applications can get developed in a shorter time duration than ReactJS.

How do I use a Vue template in JavaScript?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

How to use a template in Vue JS?

The template is inlined as a JavaScript string here, which Vue will compile on the fly. You can also use an ID selector pointing to an element (usually native <template> elements) - Vue will use its content as the template source.

What are environment variables in Vue JS?

Environment variables allow you to change this URL automatically, according to the current state of the project. With Vue.js, it is possible to use environment variables through files with the .env file extension.

How do I expose a Vue counter component to a template?

Assuming we placed our counter component inside a file called ButtonCounter.vue, the component will be exposed as the file's default export: To expose the imported component to our template, we need to register it with the components option. The component will then be available as a tag using the key it is registered under.

What is a Vue component?

When not using a build step, a Vue component can be defined as a plain JavaScript object containing Vue-specific options: The template is inlined as a JavaScript string here, which Vue will compile on the fly.


2 Answers

I just had this same problem and thought of another hack/workaround... Note I haven't tried this yet but should work.

Just use v-for with a single inline array containing your long expression, i.e.

<span v-for="item in [my.big.long.expression.item]">{{item.foo}} {{item.bar}}</span>

:)

like image 177
LMK Avatar answered Sep 19 '22 19:09

LMK


I could of course just use a computed function to do this, but in some cases it is not possible, such as when you're inside a v-for and want your computed to refer to the loop's iterator variable. In such cases, my solution is as follows:

<div :set="text=$v.form.text">
    <input 
        :class="{ error: !text.required }"
        v-model.trim="text.$model" 
    />
</div>

For those that may now be thinking, "Hey, I didn't know there was an undocumented :set in Vue", there isn't.

What I'm doing here is relying on the fact that Vue will evaluate the JavaScript of any bound attributes and I just chose to invent an unused attribute called :set.

As a reminder, a bound attribute is one that is prefixed with a : or v-bind:. The JavaScript expression inside the double quotes will be evaluated in the context of the current component and the item variable will still be available outside of the v-for in which it is being set. So, it's not a local variable, as such, rather, it's a locally assigned, component scope variable.

Make sure to add this variable to your variable declarations in the data hook.

data() { return { 
    text: '' 
}}

Here is a link to a CodePen showing this pattern at work https://codepen.io/pbastowski/pen/PXqjPG?editors=1100

like image 20
Paul Bastowski Avatar answered Sep 18 '22 19:09

Paul Bastowski