方法 1: 使用 a 标签
直接在模板中使用 a标签,并设置 target=“_blank” 来打开新页面。

<template>
  <a href="https://www.example.com" target="_blank">打开新页面</a>
</template>

方法 2: 使用window.open()
在方法中调用 window.open(),通过 JavaScript 动态打开新页面。

<template>
  <button @click="openNewPage">打开新页面</button>
</template>

<script>
export default {
  methods: {
    openNewPage() {
      window.open("https://www.aaa.com", "_blank");
    },
  },
};
</script>

方法 3: 使用 Vue Router 的 router.resolve
如果需要通过 Vue Router 打开一个新页面(例如跳转到另一个路由),可以使用 router.resolve 生成完整的 URL,然后结合 window.open 打开。

<template>
  <button @click="openNewRoute">打开新路由页面</button>
</template>

<script>
export default {
  methods: {
    openNewRoute() {
      const url= this.$router.resolve({ name: "TargetRouteName" });
      window.open(url.href, "_blank");
    },
  },
};
</script>

方法 4: 使用 的 target=“_blank”
如果需要通过 Vue Router 打开一个新页面,可以直接使用 并设置 target=“_blank”。

<template>
  <router-link :to="{ name: 'TargetRouteName' }" target="_blank">
    打开新路由页面
  </router-link>
</template>

方法 5: 动态生成a 标签
通过 JavaScript 动态创建a 标签并触发点击事件。

<template>
  <button @click="openNewPageDynamic">动态打开新页面</button>
</template>

<script>
export default {
  methods: {
    openNewPageDynamic() {
      const link = document.createElement("a");
      link.href = "https://www.aaa.com";
      link.target = "_blank";
      link.click();
    },
  },
};
</script>

方法 6: 使用 iframe(不推荐)
如果需要嵌入页面而不是打开新标签页,可以使用 iframe

<template>
  <iframe src="https://www.aaa.com" width="100%" height="400px"></iframe>
</template>
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐