当前位置:网站首页>Using global data to realize data sharing in wechat applet
Using global data to realize data sharing in wechat applet
2022-07-24 03:22:00 【richest_ qi】
List of articles
Antecedents feed
Every applet needs to be in App.js Call in App({}) Register applet instance .
App({
globalData:{
isPlayGlobal:false,
musicIdGlobal:''
}
})
There is only one... In the whole applet App example , It's all page sharing . Can pass getApp() Get globally unique App example , get App Data or functions on .
const appInstance = getApp();
const {
isPlayGlobal,musicIdGlobal} = appInstance.globalData;
Build a static resource server
- Global installation serve:
npm install -g serve - Create a new folder wherever you want :
resources.resources I'm gonna go ahead and create a new folder :images, Used to store static image resources ;resources I'm gonna go ahead and create a new folder :videos, For storing video files ;resources I'm gonna go ahead and create a new folder :audios, For storing audio files . - Start the server :
serve resources.
Applet project
The main files involved in the code are :
- app.json
- app.js
- app.wxss
- pages/index/index.json
- pages/index/index.wxml
- pages/index/index.wxss
- pages/index/index.js
- pages/music/music.json
- pages/music/music.wxml
- pages/music/music.wxss
- pages/music/music.js

app.json
{
"pages": [
"pages/index/index",
"pages/music/music"
],
"window": {
"navigationBarBackgroundColor": "#624d2e",
"navigationBarTitleText": " home page ",
"navigationBarTextStyle": "white"
},
"requiredBackgroundModes": [
"audio"
],
"style": "v2",
"sitemapLocation": "sitemap.json"
}
app.js
App({
globalData:{
isPlayGlobal:false, // Is there a song currently playing
musicIdGlobal:'' // Which song is currently playing
}
})
app.wxss
page{
height: 100%;
}
pages/index/index.json
{
"usingComponents": {
},
"navigationBarTitleText": " Playlist "
}
pages/index/index.wxml
<view class="index-container">
<view class="header">
<image src="/static/images/icon-play-square.png"></image>
<text> Play all </text>
<text>{
{musicList.length}}</text>
</view>
<scroll-view class="music-list" enable-flex scroll-y >
<view class="music-item" wx:for="{
{musicList}}" wx:key="id" bindtap="handleTap" data-musicitem="{
{item}}">
<view class="music-index">{
{index+1}}</view>
<image class="music-image" src="{
{item.picUrl}}"></image>
<view class="music-info">
<view class="musci-name">{
{item.name}}</view>
<view class="music-author">{
{item.author}}</view>
</view>
<image class="btn-more" src="/static/images/icon-more.png"></image>
</view>
</scroll-view>
</view>
pages/index/index.wxss
.index-container{
padding: 20rpx;
background:#624d2e
}
.header{
display: flex;
align-items: center;
}
.header image{
width: 28rpx;
height: 28rpx;
margin-right: 20rpx;
}
.header text{
color: #fff;
height: 28rpx;
line-height: 28rpx;
margin-right: 20rpx;
font-size: 28rpx;
}
.music-list{
margin-top: 20rpx;
color: #fff;
height: calc(100vh - 88rpx);
}
.music-item{
display: flex;
align-items: center;
height: 100rpx;
margin: 20rpx 0;
position: relative;
}
.music-item .music-index{
width: 50rpx;
font-size: 28rpx;
color: #aaa;
}
.music-item .music-image{
width: 80rpx;
height: 80rpx;
border-radius: 6rpx;
margin-right: 20rpx;
}
.music-item .music-info{
display: flex;
flex-direction: column;
}
.music-item .music-info .music-author{
font-size: 24rpx;
color: #aaa;
margin-top: 10rpx;
}
.music-item .btn-more{
width: 36rpx;
height: 36rpx;
position: absolute;
right: 0;
}
pages/index/index.js
const host = "http://localhost:3000"
Page({
data:{
musicList:[]
},
onLoad(){
this.getDataFromServer();
},
getDataFromServer(){
const result = [
{
id:"001",name:" In torrential rain ","author":" Li Ruoxi ","picUrl":host+"/images/ In torrential rain .jpg","url":host+"/audios/ In torrential rain .mp3"},
{
id:"002",name:" Happy little frog ","author":" Liu Shiming ","picUrl":host+"/images/ Happy little frog .jpg","url":host+"/audios/ Happy little frog .mp3"},
{
id:"003",name:" The king and the beggar ","author":" Hua Chenyu ","picUrl":host+"/images/ The king and the beggar .jpg","url":host+"/audios/ The king and the beggar .mp3"},
{
id:"004",name:" Chilo Levis reply ","author":" Xue Kaiqi ","picUrl":host+"/images/ Chilo Levis reply .jpg","url":host+"/audios/ Chilo Levis reply .mp3"},
{
id:"005",name:" Red sun ","author":" Li Keqin ","picUrl":host+"/images/ Red sun .jpg","url":host+"/audios/ Red sun .mp3"},
{
id:"006",name:" Happy worship ","author":" Wilber Pan Zhang Shaohan ","picUrl":host+"/images/ Happy worship .jpg","url":host+"/audios/ Happy worship .mp3"},
{
id:"007",name:" The door is unlocked ","author":" Huangpinguan ","picUrl":host+"/images/ The door is unlocked .jpg","url":host+"/audios/ The door is unlocked .mp3"},
{
id:"008",name:" Just love you ","author":" David Tao ","picUrl":host+"/images/ Just love you .jpg","url":host+"/audios/ Just love you .mp3"},
{
id:"009",name:" all ","author":" Wang Lihong ","picUrl":host+"/images/ all .jpg","url":host+"/audios/ all .mp3"},
{
id:"0010",name:" Years of friendship ","author":" Jordan Chan Zheng Yijian Thank God Lin Xiaofeng Qian Jiale ","picUrl":host+"/images/ Years of friendship .jpg","url":host+"/audios/ Years of friendship .mp3"},
{
id:"0011",name:" gentle ","author":" May day ","picUrl":host+"/images/ gentle .jpg","url":host+"/audios/ gentle .mp3"}
];
this.setData({
musicList:result});
},
handleTap(event){
const {
musicitem} = event.currentTarget.dataset;
wx.navigateTo({
url: '/pages/music/music?musicitem='+JSON.stringify(musicitem),
})
}
})
pages/music/music.json
{
"usingComponents": {
},
"navigationBarBackgroundColor": "#2f434e",
"navigationBarTitleText": " Music details "
}
pages/music/music.wxml
<view class="music-container">
<view class="music-name">{
{music.name}}</view>
<view class="music-author">{
{music.author}}</view>
<image class="arm {
{isPlay&&'arm-reset'}}" src="/static/images/arm.png"></image>
<view class="disc-container {
{isPlay&&'disc-animate'}}">
<image class="disc" src="/static/images/disc.png"></image>
<image class="music-image" src="{
{music.picUrl}}"></image>
</view>
<view class="player">
<view class="btns">
<image class="loop-btn" src="/static/images/loop.png"></image>
<image class="prev-btn" src="/static/images/prev.png"></image>
<image class="play-btn" src="{
{isPlay?'/static/images/stop.png':'/static/images/play.png'}}" bindtap="handlePlay"></image>
<image class="next-btn" src="/static/images/next.png"></image>
<image class="list-btn" src="/static/images/list.png"></image>
</view>
</view>
</view>
pages/music/music.wxss
.music-container{
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
background: #2f434e;
position: relative;
}
.music-container .music-name{
margin: 10rpx 0;
color: #fff;
font-size: 36rpx;
}
.music-container .music-author{
color: #bbb;
font-size: 28rpx;
margin: 6rpx 0;
}
.music-container .arm{
width:204rpx;
height: 358rpx;
position: relative;
left: 72rpx;
z-index: 99;
transform: rotate(-15deg);
transform-origin: 30rpx 30rpx;
transition: transform .7s linear;
}
.disc-container{
position: relative;
top: -128rpx;
width: 490rpx;
height: 490rpx;
}
.disc-container .disc{
width: 100%;
height: 100%;
}
.disc-container .music-image{
width: 270rpx;
height: 270rpx;
border-radius: 100%;
position: absolute;
left: 0;right: 0;top: 0;bottom: 0;
margin: auto;
}
.music-container .arm-reset{
transform: rotate(0deg);
}
.disc-animate{
animation: rotate 2.5s 1s linear infinite;
}
@keyframes rotate{
from{
transform: rotate(0deg);
}
to{
transform: rotate(360deg);
}
}
.player{
width: 100%;
position: absolute;
bottom: 60rpx;
}
.btns{
display: flex;
align-items: center;
justify-content: space-evenly;
}
.btns image{
width: 36rpx;
height: 36rpx;
}
.btns .play-btn,.btns .stop-btn{
width: 90rpx;
height: 90rpx;
}
pages/music/music.js
const appInstance = getApp();
Page({
data:{
isPlay:false,
music:{
}
},
onLoad(options){
const music = JSON.parse(options.musicitem);
this.setData({
music})
// wx.setNavigationBarTitle({
// title: music.name,
// })
const {
isPlayGlobal,musicIdGlobal} = appInstance.globalData;
const {
id} = this.data.music;
if(isPlayGlobal && musicIdGlobal === id) {
this.setData({
isPlay:true});
}
this.bam = wx.getBackgroundAudioManager();
this.bam.onPlay(() => {
this.setData({
isPlay:true})
appInstance.globalData.isPlayGlobal = true;
appInstance.globalData.musicIdGlobal = this.data.music.id;
})
this.bam.onPause(() => {
this.setData({
isPlay:false})
appInstance.globalData.isPlayGlobal = false;
})
this.bam.onStop(() => {
this.setData({
isPlay:false})
appInstance.globalData.isPlayGlobal = false;
})
this.bam.onEnded(() => {
this.setData({
isPlay:false});
appInstance.globalData.isPlayGlobal = false;
})
},
handlePlay(){
const isPlay = !this.data.isPlay;
this.setData({
isPlay});
this.musicControl();
},
musicControl(){
const {
isPlay} = this.data;
if(isPlay){
this.bam.src = this.data.music.url;
this.bam.title = this.data.music.name;
}else{
this.bam.pause();
}
}
})
Related links
边栏推荐
- uva1467
- The new idea 2022.2 was officially released, and the new features are nice
- Correct usage of iota in golang
- 数据湖:Apache Hudi简介
- How does the small program mall refine the operation of members?
- QT custom class uses custom parametric signals and slots
- Unity 消息推送
- JS small game running bear and cat source code
- Hcip --- BGP comprehensive experiment
- Regular expression \b \b understand word boundary matching in simple terms
猜你喜欢

FTP服務與配置

How will you answer the "Hello world" challenge written in C language?

JIRA automation experience sharing for 2 years

JS 数组 isAarray() typeof

What is IMU?

Babylon.js cool canvas background animation JS special effects

Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?

Services et configurations FTP

Realize the communication before two pages (using localstorage)

OSPF comprehensive experimental configuration
随机推荐
B. Eastern Exhibition- Codeforces Round #703 (Div. 2)
Programmers may still be programmers, and coders may only be coders
322. Change
uva1445
Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]
Gpushare. COM | how to use tensorboardx visualization tool?
MySQL sub database and sub table and its smooth expansion scheme
Binary search
CMT registration - Google Scholar ID, semantic scholar ID, and DBLP ID
Some properties of differential array operation
正则表达式 \b \B 深入浅出理解单词边界的匹配
What is the security of Treasury reverse repo
Correct usage of iota in golang
数据湖:Apache Hudi简介
Advantages, disadvantages and summary of sequence list and linked list
Cannot resolve symbol 'override' of idea clone‘
Data Lake: comparative analysis of open source data Lake schemes deltalake, Hudi and iceberg
1. Learn to understand web pages
C dynamic memory management details
FTP service and configuration