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

注释很详细,直接上代码

上一篇

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

源码:

app.json

{
  "usingComponents": {
    "auth":"/components/auth/auth"
  },
  "pages": [
    "pages/index/index"
  ]
}

app.js

App({
 globalData:{//定义全局变量
    isLoad:false
 }
})

index.wxml

<button type="default" bind:tap="inLoad">登入</button>

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


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

index.wxss

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

index.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

<slot wx:if="{{isLoad}}"></slot>

auth.js

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

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

    },
    detached() {

    },
  methods: {
    
  }
}
})


  

效果演示:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码对我眨眼睛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值