本文转载自:https://blog.csdn.net/crazywoniu/article/details/80942642javascript
vue-router传递参数分为两大类html
this.$router.push("home");
特别注意:命名路由这种方式传递的参数,若是在目标页面刷新是会出错的vue
使用方法以下:java
this.$router.push({ name: 'news', params: { userId: 123 }})
代码以下:vue-router
<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>编程
接受传递的参数:this
<template> <div> this is the news page.the transform param is {{this.$route.params.userId}} </div> </template> <script> </script>
运行效果以下:url
this.$router.push({ path: '/news', query: { userId: 123 }});
代码以下:spa
<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>
接收参数以下:.net
<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>