Vutify:如何配置VueRouter在新选项卡上打开链接? [英] Vuetify : How to configure VueRouter to open link on a new tab?

查看:35
本文介绍了Vutify:如何配置VueRouter在新选项卡上打开链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带菜单的导航栏,用户可以单击。一些链接需要打开一个新的选项卡。这就是我拥有的,我无法使其工作。

<template>
    <nav>
        <v-app-bar text app color="blue">
            <v-app-bar-nav-icon class="white--text" @click="drawer = !drawer"></v-app-bar-nav-icon>
            <v-app-bar-title class="text-uppercase white--text">
                <span class="font-weight-light">Logo</span>
                <span>Global</span>
            </v-app-bar-title>
        </v-app-bar>

        <v-navigation-drawer v-model="drawer" app class="grey lighten-1">
            <v-list>
                <img class="ml-5 mb-3" src="../assets/Logo-Blue.png" width="70" />

                <v-list-item v-for="link in links" :key="link.text" router :to="link.route" @click="go(link)">
                    <v-list-item-action>
                        <v-icon>{{ link.icon }}</v-icon>
                    </v-list-item-action>
                    <v-list-item-content>
                        <v-list-item-title> {{ link.text }} </v-list-item-title>
                    </v-list-item-content>
                </v-list-item>
            </v-list>
        </v-navigation-drawer>
    </nav>
</template>
<script>
export default {
    data() {
        return {
            drawer: true,
            links: [
                { icon: 'home', text: 'Dashboard', route: '/dashboard', newTab: false },
                { icon: 'leaderboard', text: 'Stats', route: 'www.google.com ', newTab: true },
            ]
        }
    },
    methods: {
        go(link) {
            console.log('go run ...')
            console.log(link)
            console.log(process.env.APP_URL)

            if (!link.newTab) {
                this.$router.push({ path: link.route })
            } else {
                window.open(link.route)
            }
        }
    }
}
</script>

src/outer/index.js

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export default router

我做错了什么吗?

打开新选项卡似乎有效,但它一直在添加我的本地主机URL。

推荐答案

不要将click-处理程序与v-list-item一起使用,因为这会破坏为路由生成的基础锚标记,并具有可访问性。坚持使用v-list-item内置的布线道具:

  • href:要使用<v-list-item>创建外部链接,请使用href道具。内部链接应改用to

  • target:要在单击链接后打开新窗口,请使用target="_blank"属性。

链接到外部URLv-list-item将使用href属性,如下所示:
<v-list-item href="https://google.com">Google</v-list-item>

打开新选项卡的用户将使用target="_blank"属性:

<v-list-item target="_blank" href="https://google.com">Google</v-list-item>

内部链接使用to道具而不是href

<v-list-item to="/dashboard">Dashboard</v-list-item>

也可以使用target="_blank"在新选项卡中打开它们:

<v-list-item target="_blank" to="/dashboard">Dashboard</v-list-item>

解决方案

在您的示例中,您有一组项目需要有条件地绑定前面提到的道具。这最好使用v-bind with an object(其中对象的键-值对被绑定为组件道具)来完成,它是根据每个项目的属性计算的:

  1. Compute具有要绑定的新道具(名为linkProps)的新链接数组(名为computedLinks):
export default {
  computed: {
    computedLinks() {
      return this.links.map(link => {
        const isExternalLink = url => /^((https?:)?//)/.test(url)
        const linkProps = {
          [isExternalLink(link.route) ? 'href' : 'to']: link.route,
        }
        if (link.newTab) {
          linkProps.target = '_blank'
        }
        return {
          ...link,
          linkProps,
        }
      })
    },
  },
}
  1. 更新模板使用computedLinks,将每个链接的linkProps绑定到对应的v-list-item
<v-list-item v-for="link in computedLinks" :key="link.text" v-bind="link.linkProps">
                                   👆                                        👆

demo

这篇关于Vutify:如何配置VueRouter在新选项卡上打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆