接收this.$store.params下面

一.页面跳转

  1. <template>
  2. <div>
  3. 个人中心
  4. <router-link to="/detail" tag="button"> //tag:可以更改标签类型
  5. 跳转到detail
  6. </router-link>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name:"Center"
  12. }
  13. </script>
  14. <style scoped>
  15. </style>

二.router-link动态传值

从center跳转到detail

  1. //1.center.vue--加id跳转
  2. <template>
  3. <div>
  4. 个人中心
  5. <router-link to="/detail/3232387" tag="button"> //detail后面跟的有id
  6. 跳转到detail
  7. </router-link>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name:"Center"
  13. }
  14. </script>
  15. <style scoped>
  16. </style>
  17. //2.router/index.js--配置
  18. const routes = [
  19. ...
  20. {
  21. path: '/detail/:id', //配置
  22. name: 'detail',
  23. component: () => import('@/views/Detail.vue')
  24. },
  25. ]
  26. //3.detail.vue--使用
  27. <template>
  28. <div>
  29. 详情页
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. name:"Detail",
  35. mounted(){
  36. console.log(this.$route.params) //接收
  37. var id=this.$route.params.id;
  38. }
  39. }
  40. </script>

三.传变量

  1. <router-link tag="p" :to="'/detail/'+item.id" v-for="item of playlists" :key="item.id" class="box">
  2. <p>{{item.name}}</p>
  3. </router-link>

在detail.vue中通过传递过来的id发送请求

  1. <template>
  2. <div>
  3. 详情页
  4. <img :src="imgUrl" alt="">
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name:"Detail",
  10. data(){
  11. return{
  12. imgUrl:""
  13. }
  14. },
  15. mounted(){
  16. var id=this.$route.params.id;
  17. var url=`http://192.168.14.49:5000/playlist/detail?id=${id}`;
  18. this.axios.get(url).then(res=>{
  19. this.imgUrl=res.data.playlist.creator.avatarUrl;
  20. })
  21. },
  22. activated(){
  23. },
  24. }
  25. </script>
  26. <style scoped>
  27. </style>
  28. //App.vue
  29. <keep-alive exclude="Detail">
  30. <router-view/>
  31. </keep-alive>
  32. //路由页面router/index.js--配置
  33. const routes = [
  34. ...
  35. {
  36. path: '/detail/:id', //配置
  37. name: 'detail',
  38. component: () => import('@/views/Detail.vue')
  39. },
  40. ]

*使用子组件跳转和父组件跳转是一样的

  1. //components/Top.vue
  2. <router-link :to="'/detail/'+data.id">
  3. <div class="list">
  4. <img :src="data.images.small" alt="" />
  5. <p>{{ data.title }}</p>
  6. </div>
  7. </router-link>
  8. //views/Detail.vue
  9. mounted(){
  10. var id=this.$route.params.id;
  11. this.axios.get("v2/movie/subject/"+id).then(res=>{
  12. this.url=res.data.images.small;
  13. this.summary=res.data.summary;
  14. })
  15. }