在Vue.js 3中,要实现简单路由器链路的跳转到另一个Vue页面,可以通过以下步骤进行操作:
npm install vue-router@next
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
import Home from './views/Home.vue'
import AnotherPage from './views/AnotherPage.vue'
const routes = [
{ path: '/', component: Home },
{ path: '/another-page', component: AnotherPage }
]
const router = createRouter({
history: createWebHistory(),
routes
})
const app = createApp(App)
app.use(router)
app.mount('#app')
<!-- Home.vue -->
<template>
<div>
<h1>Home Page</h1>
<router-link to="/another-page">Go to Another Page</router-link>
</div>
</template>
<script>
export default {
// 组件逻辑
}
</script>
<!-- AnotherPage.vue -->
<template>
<div>
<h1>Another Page</h1>
<router-link to="/">Go back to Home</router-link>
</div>
</template>
<script>
export default {
// 组件逻辑
}
</script>
<router-view>
标签来渲染路由对应的组件。示例代码如下:<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
// 组件逻辑
}
</script>
通过以上步骤,简单路由器链路就可以在Vue.js 3中正常工作了。在Home页面中,点击"Go to Another Page"链接将会跳转到AnotherPage页面;在AnotherPage页面中,点击"Go back to Home"链接将会返回到Home页面。
关于Vue Router的更多详细信息和用法,可以参考腾讯云的相关文档和产品介绍:
领取专属 10元无门槛券
手把手带您无忧上云