微信小程序(四十四)鉴权组件插槽-登入检测

注释很详细,直接上代码

新增内容:
1.鉴权组件插槽的用法
2.登入检测示范

源码:

app.json

json 复制代码
{
  "usingComponents": {
    "auth":"/components/auth/auth"
  }
}

app.js

js 复制代码
App({
 globalData:{//定义全局变量
    isLoad:false
 }
})

index.wxml

xml 复制代码
<button type="default" bind:tap="inLoad">登入</button>

<button type="primary" bind:tap="outLoad">退出登入</button>


<auth isLoad="{{isLoad}}"><!-- 传输数据 -->
  <view class="tip">
  登入以后可以查看的内容
  </view>
</auth>

index.wxss

css 复制代码
.tip{
  font-size: 60rpx;
  color:palegreen; 
  margin-top: 200rpx;
  padding: 0rpx 30rpx;
  background-color: bisque;
}

index.js

js 复制代码
Page({
  data:{
    isLoad:false
  },
  //登入
  inLoad(){
    //修改全局变量为true
    const app=getApp()
    app.globalData.isLoad=true
    //console.log(app.globalData.isLoad)
    
    this.setData({//修改页面数据
      isLoad:app.globalData.isLoad
    })

  },

  //退出登入
  outLoad(){
    const app=getApp()
    app.globalData.isLoad=false
    //console.log(app.globalData.isLoad)

    this.setData({
      isLoad:app.globalData.isLoad
    })
  }
})

温馨提醒,以下组件不是页面,请勿建错
auth.wxml

wxml 复制代码
<slot wx:if="{{isLoad}}"></slot>

auth.js

复制代码
Component({
  behaviors: [],
  properties: {
    isLoad: {//接收数据
      type: Boolean,
      value: false
    }
  },
  lifetimes: {
    created() {

    },
    attached() {
     
    },
    moved() {

    },
    detached() {

    },
  methods: {
    
  }
}
})

效果演示:

相关推荐
蜗牛前端2 天前
codex 全流程开发上线的高颜值礼簿小程序
前端·微信小程序
爱勇宝6 天前
我想认真做一件小事:让孩子和家长更好地互动
微信小程序·小程序·云开发
唯火锅不可辜负6 天前
避坑指南:iOS 下 scroll-view 嵌套 fixed 布局的“翻车”现场与修复
微信小程序
didiplus6 天前
运维人的随身神器:我把25个常用工具塞进了微信小程序
微信小程序
一份执念7 天前
uni-app 小程序分包限制处理与主包体积优化实战
前端·微信小程序
一份执念7 天前
ECharts 安装与使用完全指南:从全量引入到小程序分包优化
微信小程序·echarts
skiyee8 天前
🔥UniApp 仅需 5 行代码!实现所有页面中控制应用主题变化
前端·微信小程序
Jinkey9 天前
要用户手机号真的是为了打骚扰电话吗?浅谈微信生态会员账号体系与资产合并
后端·微信·微信小程序
用户43242810611411 天前
微信小程序从0到1接入微信支付的完整攻略
微信小程序
spmcor13 天前
微信小程序 setStorageSync 踩坑实录:别让"顺手一存"变成"隐形炸弹"
微信小程序