Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Opening url in json file with vue.js

I have a url key in my json file and there are values against it. When I create a button with vue.js and click this button, I want the url to be opened. How can I do it?

My code:

<template>
   <div class="q-pa-md q-gutter-sm">
    <q-btn @click="gotoUrl" color="white" text-color="black" label="Standard" />
  </div>
</template>

<script>
import json from './assets/test.json'

export default defJson({
  setup: () => ({jsonData}),
  methods: {
    gotoUrl(){
      window.open()
    }
  }
})
</script>

Json file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
  "id": "1",
  "name": "stackoverflow",
  "url": "https://stackoverflow.com/"
  }
]

When I click this button, the link needs to be read and opened in the json file.

>Solution :

First you wan to loop over the objects in your JSON array like this:

  <div v-for="site in jsonData" :key="site.id" class="q-pa-md q-gutter-sm">
    ...
  </div>

Then you can simply create elements around your buttons with the url from the site in the href:

  <div v-for="site in jsonData" :key="site.id" class="q-pa-md q-gutter-sm">
    <a :href="site.url" target="_blank">
      <q-btn color="white" text-color="black" :label="site.name" />
    </a>
  </div>

Its pretty basic vue stuff.. They have great documentation, check it out: https://vuejs.org/guide/introduction.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading