欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue编程式跳转

程序员文章站 2022-05-26 14:47:24
编程式跳转 router.js: ......

编程式跳转

<template>
  <ul class = "prolist">
    <!-- //产品 -->
    <!-- :to = "/detail/item.id" -->
    <!-- 声明式跳转 :to = "{ name: 'detail',params: { id: item.id } }" -->
    <!-- <router-link :to = "{ name: 'detail',params: { id: item.id } }" tag = "li" class = "proitem" v-for="(item,index) of iss" :key='index'>
      <div class = "itemimg">
       <img :src="item.images.small" :alt="item.alt">
      </div>
      <div class = "iteminfo">
        <h3>{{ item.title }}</h3>
        <div class = "directors">
          <span v-for="(itm,idx) of item.directors" :key="idx">
            {{ itm.name }}/
          </span>
        </div>
        <rating :rating='(item.rating.average / 2).tofixed(1)' />
      </div>
    </router-link> -->

    <!-- 编程式跳转 -->
    <!-- @click="godetail(item.id) -->
    <li class = "proitem" v-for="(item,index) of iss" @click="godetail(item.id)" :key='index'>
      <div class = "itemimg">
       <img :src="item.images.small" :alt="item.alt">
      </div>
      <div class = "iteminfo">
        <h3>{{ item.title }}</h3>
        <div class = "directors">
          导演:<span v-for="(itm,idx) of item.directors" :key="idx">
            {{ itm.name }}/
          </span>
        </div>
        <div class = "casts">
           演员:<span v-for="(itm,idx) of item.casts" :key="idx">
            {{ itm.name }}/
          </span>
        </div>
        <rating :rating="(item.rating.average / 2).tofixed(1)"/>
      </div>
    </li>
  </ul>
</template>
<script>
import rating from '@/components/common/rating'

export default {
  methods: {
    godetail (id) {
      // console.log(this.$router)
      // this.$router.push('/detail/' + id) //id由函数获得
      // this.$router.push({ name: 'detail', params: { id: id } }) // 另一种方法
      this.$router.push({ path: '/detail/' + id }) // 另一种方法
    }
  },
  props: ['iss'],
  components: {
    rating
  }
}
</script>

router.js:

{
      // path: '/detail',
      path: '/detail/:id', // 详情需要配一个id,获取遍历
      name: 'detail',
      component: () => import('./views/detail/index.vue')
    },