本文轉載自: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>