本文转载自:https://blog.csdn.net/crazywoniu/article/details/80942642
vue-router传递参数分为两大类
this.$router.push("home");
特别注意:命名路由这种方式传递的参数,如果在目标页面刷新是会出错的
使用方法如下:
this.$router.push({ name: ‘news‘, params: { userId: 123 }})
代码如下:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<button @click="routerTo">click here to news page</button>
</div>
</template>
<script>
export default {
name: ‘HelloWorld‘,
data () {
return {
msg: ‘Welcome to Your Vue.js App‘
}
},
methods:{
routerTo(){
this.$router.push({ name: ‘news‘, params: { userId: 123 }});
}
}
}
</script>
<style>
</style>
接受传递的参数:
<template> <div> this is the news page.the transform param is {{this.$route.params.userId}} </div> </template> <script> </script>
运行效果如下:
this.$router.push({ path: ‘/news‘, query: { userId: 123 }});
代码如下:
<template> <div class="hello"> <h1>{{ msg }}</h1> <button @click="routerTo">click here to news page</button> </div> </template> <script> export default { name: ‘HelloWorld‘, data () { return { msg: ‘Welcome to Your Vue.js App‘ } }, methods:{ routerTo(){ this.$router.push({ path: ‘/news‘, query: { userId: 123 }}); } } } </script> <style> </style>
接收参数如下:
<template> <div> this is the news page.the transform param is {{this.$route.query.userId}} </div> </template> <script> </script>
运行效果如下:
<router-link to="news">click to news page</router-link>
<router-link :to="{ name: ‘news‘, params: { userId: 1111}}">click to news page</router-link>
<router-link :to="{ path: ‘/news‘, query: { userId: 1111}}">click to news page</router-link>
原文:https://www.cnblogs.com/Sky-Ice/p/10443120.html