Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Adding phone prefix to phone number variable in my component

Hi everybody! I'm new to vue and was wondering what would be the best way to add a prefix to my phone number variable.

Say I in my template I have curly braces interpolation like this:

<span> {{ globals.phoneNumber }}</span>

but want to add the prefix: +34 to the value of globals.phoneNumber variable, what would be the best way to do it? I don't want to create ANOTHER variable just for this and concatenate the strings!

0 likes
9 replies
Gabotronix's avatar

So I decided to make a vue filter like this:

Vue.filter('phonePrefix', function (value) {
    return value ? '+34${value}' : null;
})

And added this to my template:

<a class="header_small_phone_number" :href="tel:{{ APP.globals.appBookingPhone | phonePrefix}} ">{{ APP.globals.appBookingPhone }}</a>

However when I compile I get this error:

 - invalid expression: Unexpected token : in

    tel: +34{{ APP.globals.appBookingPhone | phonePrefix}}

  Raw expression: :href="tel: +34{{ APP.globals.appBookingPhone | phonePrefix}} "

Also tried this but it doesn't work either:

<a class="header_small_phone_number" :href="'tel:${APP.globals.appBookingPhone | phonePrefix}'">{{ APP.globals.appBookingPhone }}</a>
D9705996's avatar

@Gabotronix did you try my solution of just putting your +34 in your template?

You originally said

I don't want to create ANOTHER variable just for this and concatenate the strings

But you have setup a filter to do just this...

Gabotronix's avatar

I just tried yours:

<a class="header_small_phone_number" :href="tel:+34{{ APP.globals.appBookingPhone }}">{{ APP.globals.appBookingPhone }}</a>

but I'm still getting a compilling error

D9705996's avatar

You are trying to bind href attribute which means the contents are parsed as JavaScript so try

<a class="header_small_phone_number" :href="'tel: +34' + {{ APP.globals.appBookingPhone }}">
  {{ APP.globals.appBookingPhone }}
</a>

Please remember when you ask a question to much sure you provide the correct detail to prevent getting mislead. Your original post referenced a span but your actual question was about an href attribute on a link.

Cronix's avatar

try

:href="tel:+34 {{APP.globals.appBookingPhone}}"

or maybe it needs to be

:href="tel:+34 ${APP.globals.appBookingPhone}"
rawilk's avatar

As far as I'm aware, you can't use curly brackets curly brackets inside of attributes. This would work instead:

:href="`tel:+34 ${APP.globals.appBookingPhone}`"

Edit: By curly brackets, I mean the double curly brackets.

D9705996's avatar
D9705996
Best Answer
Level 51

To be honest in reflection of your entire problem just create a computed property

computed: {
   phoneLink: function(){ 
      return 'tel:+34' + APP.globals.appBookingPhone;
   }

...

<a class="header_small_phone_number" :href="phoneLink" ...

Depending on where your APP variable has been declared you might need to use this.APP

Please or to participate in this conversation.