溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Vue中實現(xiàn)一個編程式跳轉功能

發(fā)布時間:2021-03-01 15:47:07 來源:億速云 閱讀:264 作者:戴恩恩 欄目:web開發(fā)

這篇文章主要為大家詳細介紹了如何在Vue中實現(xiàn)一個編程式跳轉功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,發(fā)現(xiàn)的小伙伴們可以參考一下:

vue是什么軟件

Vue是一套用于構建用戶界面的漸進式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應用,其核心庫只關注視圖層,方便與第三方庫和項目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復雜的單頁應用。

編程式跳轉的實現(xiàn)代碼,如下所示:

<template>
 <ul class = "prolist">
  <!-- //產(chǎn)品 -->
  <!-- :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">
    <h4>{{ item.title }}</h4>
    <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">
    <h4>{{ item.title }}</h4>
    <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由函數(shù)獲得
   // 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')
  },

ps:下面看下vue 編程式js跳轉路由

請看goNews()方法

<template>
  <!-- 所有的內(nèi)容要被根節(jié)點包含起來 -->
  <div id="home">  
    我是首頁組件
    <button @click="goNews()">通過js跳轉到新聞頁面</button>
  </div>
</template>
<script>
  export default{
    data(){
      return {        
        msg:'我是一個home組件'
      }
    },
    methods:{
      goNews(){
        // 注意:官方文檔寫錯了
        //第一種跳轉方式
        // this.$router.push({ path: 'news' })
        // this.$router.push({ path: '/content/495' });
        //另一種跳轉方式
          //  { path: '/news', component: News,name:'news' },
          // router.push({ name: 'news', params: { userId: 123 }})
          this.$router.push({ name: 'news'})
      }
    }
  }
</script>
<style lang="scss" scoped>
</style>

以上就是億速云小編為大家收集整理的如何在Vue中實現(xiàn)一個編程式跳轉功能,如何覺得億速云網(wǎng)站的內(nèi)容還不錯,歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

vue
AI