Vue可以通过以下几种方式实现页面跳转:

  1. 使用<router-link>组件:<router-link>是Vue Router提供的组件,可以用于创建导航链接。通过设置to属性来指定目标路由,点击链接后会自动跳转到目标路由对应的页面。

    <pre><code class="html"><template>
    

    <div> <router-link to="/home">跳转到首页</router-link> <router-link :to="{ path: '/about' }">跳转到关于页面</router-link> </div> </template>

  2. 使用编程式导航:在Vue组件中通过$router对象进行跳转。可以使用this.$router.push()方法跳转到目标路由。

    <pre><code class="javascript">// 在Vue组件中使用
    

    methods: { goToHome() { this.$router.push('/home'); }, goToAbout() { this.$router.push({ path: '/about' }); } }

  3. 使用<a>标签:可以直接使用<a>标签来实现页面跳转,但是这种方式会刷新整个页面,不适用于单页应用。

    <pre><code class="html"><a href="/home">跳转到首页&lt;/a&gt;
    

    <a href="/about">跳转到关于页面</a>

注意:上述方式都需要配合Vue Router来进行页面跳转。

Vue 页面跳转实现方法 | Vue Router 使用指南

原文地址: https://www.cveoy.top/t/topic/qBeV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录