在 vue.js 中,有以下三种打开新页面的方法:1. 使用 组件;2. 使用 $router.push() 方法;3. 使用 window.location.href 属性。

Vue 中打开新页面的方法
在 Vue.js 中,有以下几种打开新页面的方法:
1. 使用 <router-link></router-link> 组件
<router-link></router-link> 组件是 Vue Router 中的内置组件,用于在单页面应用程序中导航。它可以用来打开新的页面或导航到现有页面。
立即学习“前端免费学习笔记(深入)”;
示例:
CRMEB单商户商城打通版基于Thinkphp6.0+vue+mysql+redis开发,前后台全部采用前后端分离式开发。前端框架为uni-app,多端合一,首页页面后台可视化编辑操作,后台采用iview框架。
<code class="vue"><template>
<router-link to="/new-page">Open New Page</router-link>
</template>
<script>
import { defineComponent } from 'vue';
import { useRouter } from 'vue-router';
export default defineComponent({
setup() {
const router = useRouter();
return {
router,
};
},
});
</script></code>2. 使用 $router.push() 方法
$router.push() 方法用于编程方式导航到一个新的页面。它将一个新的路由条目推送到路由堆栈中,从而打开新的页面。
示例:
<code class="js">this.$router.push('/new-page');</code>3. 使用 window.location.href
window.location.href 属性可以用来设置或获取当前页面的 URL。通过直接修改此属性,可以打开一个新的页面。
示例:
<code class="js">window.location.href = '/new-page';</code>







