Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a method from html attribute in vue js

I have a form and I need to call a method from a placeholder and also from other types of html attribute.

Is there anyways I can call a vue method? Here is what I am trying to do

<input type="text" class="form-control" v-model="user.userName" 
 placeholder=t("un") required> // want to call method t() from the placeholder

It seems this method cannot be called this way. Is there any other ways to achieve this?

And my method is

methods: {
   t(key){
        console.log(key)
        var local='fr';
        return this.trans(key,local);
      }
}
like image 292
Hkm Sadek Avatar asked Sep 30 '17 09:09

Hkm Sadek


People also ask

How do you call a method in Vue component?

In short, for vue js call method from parent component in child, assign ref to the component. The other way around – call from the child in the parent component, emit an event in child and listen to it in the parent component.

How do you call a method on page load Vue?

We can call a Vue. js method on page load by calling it in the beforeMount component hook. We can also make a method run on page load by calling it in the created hook. And we can do the same in the mounted hook.

How do you link Vue and HTML?

The simplest way to get started with Vue is to grab the development version script for it and add it to the head tag of your HTML file. Then you can start Vue code inside the HTML file inside of script tags. And have the Vue code connect up with an existing element on your HTML page.

How do I define a method in Vue?

We can define methods on a Vue instance by passing an object to the methods property. Vue methods are similar to JavaScript functions and allow us to add interactivity to our Vue apps.


1 Answers

Use v-bind (https://v2.vuejs.org/v2/api/#v-bind)

<input type="text" class="form-control" v-model="user.userName" 
 v-bind:placeholder="t('un')" required>
like image 57
Gabriele Petrioli Avatar answered Sep 30 '22 01:09

Gabriele Petrioli