1

I have an item.channelId that I have like to use in href. It should achieve something like this:

          <a :href="https://youtube.com/${item.channelId}">{{
            item.channelTitle
          }}</a>

How can I put item.channelId into href?

1
  • 2
    use this syntax: :href="`https://youtube.com/${item.channelId}`" Commented Sep 17, 2020 at 13:05

3 Answers 3

5

Try using

 <a :href="'https://youtube.com/'+item.channelId">{{item.channelTitle}}</a>

In the above example the property value href is treated as the string sum of 'https://youtube.com/' and the value in the variable item.channelId

If you want to make use of template literals, try

<a :href="`https://youtube.com/${item.channelId}`">{{item.channelTitle}}</a>

In case of template literals, the variable values are written inside ${} syntax and the entire string will be enclosed inside backticks (` `)

Sign up to request clarification or add additional context in comments.

Comments

2

You can use backtick to escabe variable inside of a string like this

<a :href="`https://youtube.com/${item.channelId}`">
    {{item.channelTitle}}
</a>

Comments

1

It should be:"`https://youtube.com/${item.channelId}`" with backticks.

You can check here: Template Literals

I think you are trying to use Template Literals. You should surround your string with backticks for it to work and not just "".

Everything inside backticks will be considered a string aside from anything with ${variable-name} will be considered a variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.