vue router programmatic navigation

vue router programmatic navigation

1. Jump to the page through js

Navigation has router-link

1.1 Create a new Page.vue file

<template>
    <button @click="goPage">js跳转页面-按钮</button>
</template>

<script>
export default {
    
    
    methods: {
    
    
        goPage() {
    
    
            //console.log(this.$router) //打印$router
            //this.$router.push('/') // 字符串路径
            //this.$router.push({path:"/"}) // 带有路径的对象
            //this.$router.push({path:'/user/123'}) //带有路径对象的参数
            //this.$router.push({name:'user',params:{id:1234}})//另一种形式
            //this.$router.push({path:"about",query:{name:"LLT"}})//带查询参数?
            //替换当前位置
            //this.$router.push({path:"about",query:{name:"LLT"},replace:true})
            this.$router.replace({
    
     path: "about", query: {
    
     name: "LLT1" } })
        }
    }
}
</script>

1.2 Configure router index.js

const routes = [
    {
    
    path:'/page',component:Page},//js跳转页面
]

1.3 Configure App.vue

<router-link to="/page">js跳转页面</router-link>

2. Replace the current position

this.$router.push({
    
    path:"about",query:{
    
    name:"LLT"},replace:true})
this.$router.replace({
    
    path:"about",query:{
    
    name:"LLT1"}})

3. Take a step back

this.$router.go(-1)
this.$router.back()//后退,等于go(-1)
this.$router.forword() //前进,等于go(1)

Guess you like

Origin blog.csdn.net/Linlietao0587/article/details/128379680