第一页 点击去第二页的时候进行传值直接贴代码看:javascript
<template>
<div id="app">
<div><router-link to="/">首页</router-link></div>
<div><a href="javascript:void(0)" @click="getMovieDetail(1)">去第二个页面</a></div>
<div><router-link to="/home">去home</router-link></div>
<router-view/>
<a href="https://www.feiyit.com">abc</a>
</div>
</template>
<script>
export default {
name: 'app',
methods:{
getMovieDetail(id) {
this.$router.push({
name: 'login',
params: {
id: id
}
})
}
}
}
</script>
<style>
</style>
红色部分为传值部分,使用this.$router.push()进行传值html
第二页面接收从第一个页面传递过来的参数 idjava
<template>
</template>
<script>
export default {
name: 'login',
data () {
return {
uid:this.$route.params.id,
msg: '这是第二个页面'
}
},
computed: {
},
mounted: function() {
console.log(this.uid);
},
methods:{
}
}
</script>
红色部分打印下传递过来的id值,在接受传值的页面里使用this.$route.params.id 便可获取到传递的值app
注意,若是刷新login页面,将失去传值ui
解决方法,在路由里面增长变量 其中【/login/:id】 this
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/login/:id',
name: 'login',
component: login,
meta: {
title: '登陆页'
}
},
{
path: '/home',
name: 'home',
component: home,
meta: {
title: '其余页'
}
}
]
})