主頁 「去第二個頁面」方法傳值1javascript
<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> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
login.vue頁面接收值
在控制檯可查看是否接受
<template> <p>{{msg}}aaaaaaaaaaa</p> </template> <script> export default { name: 'login', data () { return { uid:this.$route.params.id, msg: '這是第二個頁面' } }, computed: { theme() { return this.$store.getters.THEME_COLOR } }, mounted: function() { console.log(this.uid); }, methods:{ } } </script>
控制檯輸出結果vue
重點,若是刷新login頁面,將失去傳值java
解決方法,在路由裏面增長變量 其中【/login/:id】 web
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: '其餘頁' } } ] })
Demo 奉上 https://pan.baidu.com/s/1eRFWTvcapp