詳解uniapp頁面跳轉URL傳參大坑
更新時間:2022年03月11日 09:44:01 作者:未月廿三
本文主要介紹了詳解uniapp頁面跳轉URL傳參大坑,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
案例
展示電影詳情,傳遞電影的id.從search.vue傳遞到movie.vue
methods: { showMovie(e){ var trailerid = e.currentTarget.dataset.trailerid; // console.log(trailerid); uni.navigateTo({ url: '../movie/movie?trailerId='+trailerid, success: res => {}, fail: () => {}, complete: () => {} }); } }
search.vue全部文件
<template> <view class="page"> <view class="search-block"> <view class="search-ico-wrapper"> <image src="../../static/icos/search.png" class="search-ico"></image> </view> <input type="text" value="" placeholder="請輸入電影名稱" maxlength="10" class="search-text" confirm-type="search" @confirm="searchMe" /> </view> <view class="movie-list page-block"> <view v-for="movie in resultList" :key="movie.id" class="movie-wrapper"> <image :src="movie.cover" :data-trailerId="movie.id" @click="showMovie" class="poster"></image> </view> <!-- <view class="movie-wrapper"> <image src="../../static/poster/civilwar.jpg" class="poster"></image> </view> --> </view> <view class="bottom-tip" v-if="show"> 親,已經到底了! </view> </view> </template> <script> import {DataMixin} from "../../common/DataMixin.js" export default { mixins:[DataMixin], data() { return { keyWords: '', show: false, resultList: [] } }, onLoad() { this.resultList = this.list; }, onPullDownRefresh(e) { uni.showLoading({ mask: true }); uni.showNavigationBarLoading(); this.resultList = this.list; this.show = false; this.queryByKeyWords(); uni.stopPullDownRefresh(); uni.hideLoading(); uni.hideNavigationBarLoading(); }, onReachBottom() { uni.showLoading({ mask: true }); uni.showNavigationBarLoading(); this.resultList = [...this.list, ...this.appendList]; this.show = true; uni.stopPullDownRefresh(); uni.hideLoading(); uni.hideNavigationBarLoading(); }, methods: { showMovie(e){ var trailerid = e.currentTarget.dataset.trailerid; uni.navigateTo({ url: `../movie/movie?trailerId=${trailerid}`, success: res => {}, fail: () => {}, complete: () => {} }); }, queryByKeyWords(){ var tempList = [...this.list, ...this.appendList]; this.resultList = []; if (this.keyWords) { tempList.forEach(movie => { if (movie.name.indexOf(this.keyWords) != -1) { this.resultList.push(movie) } }) } else { this.resultList = this.list; } }, searchMe(e) { this.show = false; var value = e.detail.value; this.keyWords = value; this.queryByKeyWords(); } } } </script> <style> @import url("search.css"); </style>
movie接收參數(shù)
<template> <view class="page"> <!-- 視頻播放start --> <view class="player"><video :src="movieSingle.trailer" :poster="movieSingle.poster" class="movie" controls></video></view> <!-- 視頻播放end --> <!-- 影片基本信息start --> <view class="movie-info"> <image :src="movieSingle.cover" class="cover"></image> <view class="movie-desc"> <view class="title">{{ movieSingle.name }}</view> <view class="basic-info">{{ movieSingle.basicInfo }}</view> <view class="basic-info">{{ movieSingle.originalName }}</view> <view class="basic-info">{{ movieSingle.totalTime }}</view> <view class="basic-info">{{ movieSingle.releaseDate }}</view> <view class="score-block"> <view class="big-score"> <view class="score-words">綜合評分:</view> <view class="movie-score">{{ movieSingle.score }}</view> </view> <view class="score-stars"> <block v-if="movieSingle.score >= 0"><trailer-stars :innerScore="movieSingle.score" showNum="0"></trailer-stars></block> <view class="prise-counts">{{ movieSingle.priseCounts }}點贊</view> </view> </view> </view> </view> <!-- 影片基本信息end --> </view> </template> <script> import trailerStars from '../../components/trailerStars/trailerStars.vue'; import { DataMixin } from '../../common/DataMixin.js'; export default { name: '', mixins: [DataMixin], components: { trailerStars }, data() { return { movieSingle: {}, trailerId: '' }; }, onLoad(params) { this.trailerId = params.trailerId; var tempList = [...this.list, ...this.appendList]; tempList.forEach(movie => { if (movie.id == this.trailerId) { this.movieSingle = movie; } }); }, methods: {} }; </script> <style> @import url('movie.css'); </style>
詳解
1.因為引入了組件trailerStars,此組件依賴onLoad接收的trailerId,然后去查詢獲取movie的詳情.
2.此時trailerStars組件已經加載完畢,但是movie詳情還沒獲取,就會產生movie.score為undefined的情況,此時需要處理
處理
首先只有movieSingle.socre >= 0時才加載組件
<block v-if="movieSingle.socre >= 0"><trailer-stars :innerScore="movieSingle.socre" showNum="0"></trailer-stars></block>
同時,trailerStars加載的時候需要放在mounted中加載
<template> <view class="movie-score-wrapper"> <image v-for="yellow in yelloScore" src="../../static/icos/star-yellow.png" class="star-ico"></image> <image v-for="gray in grayScore" src="../../static/icos/star-gray.png" class="star-ico"></image> <view class="movie-score" v-if="showNum==1">{{innerScore}}</view> </view> </view> </template> <script> export default { name: "trailerStars", props: { innerScore: 0, //外部傳入的分數(shù) showNum: 0, //是否顯示,1顯示,0不顯示 }, data() { return { yelloScore: 0, grayScore: 0, } }, mounted() { console.log("this.innerScore=" + this.innerScore) var tempScore = 0; if (this.innerScore != null && this.innerScore != undefined && this.innerScore != '') { tempScore = this.innerScore; } var yelloScore = parseInt(tempScore / 2); var grayScore = 5 - yelloScore; this.yelloScore = yelloScore; this.grayScore = grayScore; } } </script> <style> .movie-score-wrapper { display: flex; flex-direction: row; } .star-ico { width: 20rpx; height: 20rpx; margin-top: 6rpx; } .movie-score { font-size: 12px; color: #808080; margin-left: 8rpx; } </style>
到此這篇關于詳解uniapp頁面跳轉URL傳參大坑的文章就介紹到這了,更多相關uniapp URL傳參內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
人人網javascript面試題 可以提前實現(xiàn)下
JavaScript面試題要求:以下題目必須從一至四題中,選出三道題,使用原生代碼實現(xiàn),不可使用任何框架,第五題為選作題2012-01-01underscore之Chaining_動力節(jié)點Java學院整理
本文通過文字說明與代碼的形式給大家介紹了underscore之Chaining的相關知識,感興趣的朋友一起學習吧2017-07-07