js页面跳转
App.vue
<script setup>
</script>
<template>
<div>
<h1>Hello App!</h1>
<!--使用 router-link 组件进行导航 -->
<!--通过传递 `to` 来指定链接 -->
<!--`<router-link>` 将呈现一个带有正确 `href` 属性的 `<a>` 标签-->
<router-link to="/">Go to Home</router-link>
<p>---</p>
<router-link to="/about">Go to About</router-link>
<p>---</p>
<router-link to="/user/56">Go to User</router-link>
<p>---</p>
<router-link to="/news/56">Go to User</router-link>
<p>---</p>
<router-link to="/parent">Go to Parent</router-link>
<p>---</p>
<router-link to="/page">Go to Page</router-link>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
</template>
Page.vue
<template>
<div>
<h2>PAge web</h2>
<button @click="goPage">跳转页面</button>
</div>
</template>
<script>
export default {
name: "Page",
methods:{
goPage: function (){
console.log("Page-function")
console.log(this.$router)
//this.$router.push('/')
this.$router.push({name:"news",params:{id:56}})
//this.$router.push({path:"/user/56"})
// if(11==11){
// this.$router.push('/parent')
// }
}
}
}
</script>
<style scoped>
</style>
index.js
// 1. 定义路由组件.
// 也可以从其他文件导入
import Home from "../views/Home.vue";
import About from "../views/About.vue";
import User from "../views/User.vue";
import NotFound from "../views/NotFound.vue";
import News from "../views/News.vue";
import Parent from "../views/Parent.vue";
import Styleone from "../views/Styleone.vue";
import Styletwo from "../views/Styletwo.vue";
import Page from "../views/Page.vue";
import {createRouter, createWebHashHistory} from "vue-router";
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/user/:id', component: User },
{
name: "news",
//path: '/news/:id(\\d+)',//正则匹配
// path: '/news/:id+',//多个参数
//path: '/news/:id+',//参数可有可无
//path: '/news/:id*',//参数可重复叠加
path: '/news/:id?',//参数不可重复叠加
component: News
},
{
path: '/:path(.*)',
component: NotFound
},//使用正则,匹配任意path
{
path: "/parent",
component: Parent,
children: [
{
path: "styleone",
component: Styleone
},
{
path: "styletwo",
component: Styletwo
}
]
},
{
path: "/page",
component: Page
}
]
// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置,但我们在这里
// 暂时保持简单
const router = createRouter({
// 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
history: createWebHashHistory(),
routes, // `routes: routes` 的缩写
})
export default router
通过对象
<script> export default { name: "Page", methods:{ goPage: function (){ console.log("Page-function") console.log(this.$router) //this.$router.push('/') this.$router.push({name:"news",params:{id:56}}) //this.$router.push({path:"/user/56"}) // if(11==11){ // this.$router.push('/parent') // } } } } </script>
带参数
<script> export default { name: "Page", methods:{ goPage: function (){ console.log("Page-function") console.log(this.$router) this.$router.push({path:"/user/56"}) } } } </script>
<script>
export default {
name: "Page",
methods:{
goPage: function (){
console.log("Page-function")
console.log(this.$router)
//this.$router.push('/')
//this.$router.push({path:"/user/56"})
if(11==11){
this.$router.push('/parent')
}
}
}
}
</script>
- 带问号
<script>
export default {
name: "Page",
methods:{
goPage: function (){
console.log("Page-function")
console.log(this.$router)
this.$router.push({path:"/about",query:{name:"tom"}})
}
}
}
</script>
替换当前位置
<script>
export default {
name: "Page",
methods:{
goPage: function (){
console.log("Page-function")
console.log(this.$router)
//this.$router.push('/')
//this.$router.push({name:"news",params:{id:56}})
//this.$router.push({path:"/user/56"})
// if(11==11){
// this.$router.push('/parent')
// }
//this.$router.push({path:"/about",query:{name:"tom"}})
//替换当前位置
this.$router.push({path:"/about",query:{name:"tom"},replace: true})
}
}
}
</script>
当点击回退时,正常应该返回 /page
但是配置了替换页面会到 /
About.vue
<template>
<div>
adout
</div>
<button @click="goBack">back</button>
</template>
<script>
export default {
name: "About",
methods:{
goBack(){
//前进为1,后退为-1
this.$router.go(-1)
//this.$router.back()//=go(-1) 后退一步
//this.$router.forword()//=go(1)前进一步
}
}
}
</script>
<style scoped>
</style>
<script>
export default {
name: "Page",
methods:{
goPage: function (){
console.log("Page-function")
console.log(this.$router)
//this.$router.push('/')
//this.$router.push({name:"news",params:{id:56}})
//this.$router.push({path:"/user/56"})
// if(11==11){
// this.$router.push('/parent')
// }
this.$router.push({path:"/about",query:{name:"tom"}})
//替换当前位置
//this.$router.push({path:"/about",query:{name:"tom"},replace: true})
//this.$router.replace({path:"/about",query:{name:"tom"}})//跟上面一样
}
}
}
</script>
后退1步
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。