sundar
sundar

Reputation: 41

how to concatenate string and variable in Vue js

How to concatenate string with two adding variable use Vuejs. i have used below code but string i am not able to concatenate to variables. please check and solve my issue.

<div id = "vue_det">
    <h1>Firstname : {{firstname}}</h1>
    <h1>Lastname : {{lastname}}</h1>
    <h1>{{test()}}</h1>
    </div>
    <script type = "text/javascript" src = "js/vue_instance.js"></script>
    <script>

     var  vm = new Vue({
   el: '#vue_det',
   data: {
      firstname : 10,
      lastname  : 20,

   },
   methods: {
      test : function() {

         return "result"+ this.firstname+this.lastname;
      }
   }
})
     </script>
   </body>

Upvotes: 2

Views: 10230

Answers (2)

Hashem Ahmed
Hashem Ahmed

Reputation: 111

you need to add your string and variable in (): something like this example for key

<li v-for="(x,i) in DataTable" :key="('item-' + x.Id)"></li>

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44288

you don't want a method, you want a computed.

computed: {
      test : function() {

         return "result = "+ (this.firstname+this.lastname);
      }
   }

then use it as if it wasn't a method, but another property

{{test}}

see https://v2.vuejs.org/v2/guide/computed.html for comprehensive documentation on computed properties.

Upvotes: 3

Related Questions