2

I want to output a hyperlink from an object. Is it possible to do so?

I currently have this in my store.js, would like to use this info to output

it in a modal on my website. I got it to display it, but the a href is outputting as text.

Checked this, but none of these solutions worked for me: Adding hyperlink in Javascript array of objects

export default {
  photos: [
    {
      name: "fox1",
      image: "fox01.jpg",
      id: 1,
      header: "Photoshoot with Fox Apparel",
      description:
        "Photoshoot with " +
        '<a href="https://fox.jp/" target="_blank">Fox</a>' +
        ", a sustainably - driven e - retailer from Canada."
    }
]
}
4
  • 4
    did you try v-html vuejs.org/v2/guide/syntax.html#Raw-HTML Commented May 6, 2020 at 4:34
  • I think your best bet is to extract the link into a variable and then put that into an anchor tag in your template, vue reads your data as string it can't know it's html Commented May 6, 2020 at 5:21
  • Can't you just feed the property with the link into a <a> tag? Commented May 6, 2020 at 20:14
  • Thank you, tried v-html but that outputs everything within the object as text (such as [ { "name": "fox", "image": "fox01.jpg", "id": 1, "header": "Photoshoot with Fox Apparel"}]. Commented May 7, 2020 at 12:20

1 Answer 1

0

here is the solution: https://nexladder.com/vuejs-tutorial/vuejs-v-html-directive

<!DOCTYPE html>
    <html lang="en">
    <head>
       <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
         <div id="app">
           <div v-html="content"></div>
         </div>
         <script>
         new Vue ({
              el: '#app',
        data: {
                content: '<h2>Hello World</h2>'
        }
                 })
       </script>
     </body>
    </html>
Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that this can open up your site to XSS vulnerabilities. Don't use this if the content is coming from untrusted users.

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.