vue怎么跳转到新页面
时间: 2023-09-03 18:15:37 浏览: 51
在 Vue 中跳转到新页面可以通过 Vue Router 来实现。具体步骤如下:
1. 在 main.js 中导入 VueRouter 并进行配置:
```
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
routes: [
// 配置路由
]
})
new Vue({
router
}).$mount('#app')
```
2. 在 routes 数组中配置需要跳转的路由:
```
const routes = [
{
path: '/home',
component: Home
},
{
path: '/about',
component: About
}
]
```
3. 在需要跳转的组件中使用 `router-link` 或 `this.$router.push` 方法来跳转到新页面:
```
<!-- 使用 router-link 组件 -->
<router-link to="/home">Go to Home</router-link>
<!-- 使用 this.$router.push 方法 -->
<button @click="goToAbout">Go to About</button>
methods: {
goToAbout() {
this.$router.push('/about')
}
}
```
以上是基本的跳转到新页面的操作。如果想要传递参数,可以在路由配置中使用 `props` 属性来传递参数,或者在 URL 中使用查询字符串来传递参数。