简体   繁体   English

使用 vue.js 在 json 文件中打开 url

[英]Opening url in json file with vue.js

I have a url key in my json file and there are values against it.我的 json 文件中有一个url key ,并且有针对它的values When I create a button with vue.js and click this button, I want the url to be opened.当我使用vue.js创建一个buttonclick此按钮时,我希望打开 url。 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: Json 文件:

[
  {
  "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.单击此按钮时,需要在 json 文件中读取并打开该链接。

First you wan to loop over the objects in your JSON array like this:首先,您要遍历 JSON 数组中的对象,如下所示:

  <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:然后,您可以使用 href 中的站点中的 url 简单地在按钮周围创建元素:

  <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它非常基本的 vue 东西.. 他们有很好的文档,请查看: https://vuejs.org/guide/introduction.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM