(七)vue-router以编程模式js的方式实现( 命名路由跳转,普通路由跳转 ),以及动态路由传值和get传值的实现,路由别名
1:router.js的配置和(六)小节的完全一致
2: button按钮跳转
<button @click="gohone('ssww',no)">编程模式js跳转路由</button>
<button @click="gohone()">js编程模式调js函数跳转</button>
(1):动态路由传值
<button @click="`this.$router.push({ path: '/Details/22/${no}'})`">编程模式js路由跳转实现动态路由传值</button>
<button @click="this.$router.push({ path: '/Details/2211/6655' })"> 编程模式js路由跳转实现动态路由传值 </button>
<button @click="this.$router.push({ path: '/n1/2211/6655' })"> 别名的方式跳转并且实现动态路由传值 </button>
<button @click="this.$router.push({name: 'DetailsRoute', params: { id: '3322', no: '45' } })">命名路由实现动态路由传值 </button>
(2):get传值
<button @click="this.$router.push({ path: '/getdetail', query: { id: 55, no: 322 } })">编程模式js路由跳转实现get传值</button>
<button @click="this.$router.push({ path: '/n1', query: { id: 55, no: 322 } })">别名的方式跳转并且实现get传值</button>
<button @click="this.$router.push({ name: 'GetdetailRoute', query: { id: '3322', no: '45' }})">命名路由实现get传值</button>
3:在methods里面定义gohone方法
<script>
export default {
name: "App",
components: {},
methods: {
gohone() {
this.$router.push({
path: "/NewsPath",
});
},
},
};
</script>
3:接收页接收参数
(1)如果是get传参过来的,用this.$route.query.id方式获取传过来的参数
export default {
name: 'GetDetailFrom',
props: {
},mounted(){//页面加载完后执行得钩子事件
alert(this.$route.query.id)
alert(this.$route.query.no)
}
};
(2)如果是动态路由传参过来的,用this.$route.params.id方式获取传过来的参数
export default {
name: 'DetailFrom',
props: {
},mounted(){//页面加载完后执行得钩子事件
alert(this.$route.params.id)
alert(this.$route.params.no)
}
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现