详解uniapp页面跳转URL传参大坑

作者:未月廿三 时间:2023-09-15 09:52:43 

案例

展示电影详情,传递电影的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接收参数

<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, //外部传入的分数
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>

来源:https://www.cnblogs.com/eternityz/p/12270011.html

标签:uniapp,URL,传参
0
投稿

猜你喜欢

  • js数组的基本用法及数组根据下标(数值或字符)移除元素

    2024-04-10 10:40:26
  • 浅谈python中的__init__、__new__和__call__方法

    2023-01-05 21:21:03
  • Python编程中运用闭包时所需要注意的一些地方

    2021-10-27 06:07:21
  • vue基础语法之插值表达式详解

    2024-05-10 14:10:48
  • python神经网络学习使用Keras进行回归运算

    2023-01-30 14:08:45
  • OpenCV实战之AI照片背景替换

    2022-06-11 06:58:09
  • SQL Server数据库中的表名称、字段比较

    2024-01-13 17:02:24
  • selenium3+python3环境搭建教程图解

    2022-09-04 14:47:16
  • python RabbitMQ 使用详细介绍(小结)

    2022-11-18 21:29:33
  • Python使用win32com.client的方法示例

    2021-03-22 14:32:48
  • 解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available

    2023-05-03 22:58:56
  • Python 新建文件夹与复制文件夹内所有内容的方法

    2022-03-22 00:05:05
  • 基于python实现垂直爬虫系统的方法详解

    2023-11-17 13:33:38
  • 在pyCharm中下载第三方库的方法

    2022-12-17 10:37:16
  • Python利用第三方模块实现压缩css文件

    2023-04-28 07:21:02
  • python中的import、from import及import as的区别解析

    2022-10-07 15:56:09
  • python定时检查启动某个exe程序适合检测exe是否挂了

    2021-04-29 13:59:36
  • python使用webbrowser浏览指定url的方法

    2023-10-24 03:33:31
  • python双向链表原理与实现方法详解

    2021-03-06 05:29:39
  • 使用Python实现毫秒级抢单功能

    2022-10-17 18:46:16
  • asp之家 网络编程 m.aspxhome.com