声明式页面跳转 使用的是内置组件
点击跳转到/home 命令式页面跳转 使用js来实现页面跳转
<template>
<div>
智能应用
<button @click='back'>返回</button>
<button @click="gotopush">去指定页面</button>
<button @click="gotoreplace">replace</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import{useRouter} from 'vue-router'
export default defineComponent({
setup() {
const router =useRouter()
console.log(router)
const back = () => {
router.back() // 底层是go(-1)
};
const gotopush=()=>{
// router.push('/home_1')
router.push({name:'list',params:{id:1},query:{a:1,title:'衣服'}})
}
const gotoreplace=()=>{
// router.replace('/home_1')
router.replace({name:'list',params:{id:1},query:{a:1,title:'衣服'}})
}
return {
back,gotopush,gotoreplace
};
},
});
</script>
push
里的参数和声明式页面跳转的:to=""
参数类似
push和replace区别:push会将我们的跳转操作放入浏览器的历史记录,我们可以通过浏览器的前进和后退按钮进行路由的访问和跳转