老莫的笔记本  
  
查看: 802|回复: 0

Remax 实际开发种的使用

[复制链接]

662

主题

878

帖子

5139

积分

超级版主

Rank: 8Rank: 8

积分
5139
发表于 2021-2-25 09:54:51 | 显示全部楼层 |阅读模式
本帖最后由 周大胖子 于 2021-2-25 15:45 编辑

1. 判断登录状态,未登录跳转登录页 ;[ 也就是判断token,且这个token为全局数据 ]  纵观全局 ,这样最方便
  1. import * as React from 'react';
  2. import { View } from 'remax/one';
  3. import store from '../../store/Store';
  4. import { funGetNewsList } from '../../store/ActionCreator';
  5. import { navigateTo  } from 'remax/wechat';
  6. import { redirectTo } from 'remax/one';
  7. let LoginUrl = '../login/login';

  8. class Shop extends React.Component{
  9.     constructor(props) {
  10.         super(props);

  11.         // 接收所有数据,然后进行过滤,取出新闻的部分
  12.         let allStore = store.getState();
  13.         this.state = allStore;
  14.         this.change = this.change.bind(this);
  15.         this.lmStoreChange = this.lmStoreChange.bind(this);
  16.         this.m =1;
  17.         // console.log( allStore )
  18.         // 当我写在 change 里时,store.subscribe 会产生多个BUG 但是在这里却正常?
  19.         store.subscribe( this.lmStoreChange ) ;
  20.     }

  21.     componentDidMount(){
  22.         if( this.state.token ){
  23.             console.log( ' 这是已经登录了' );

  24.         }else{
  25.             redirectTo({
  26.                 url: LoginUrl
  27.             })
  28.             console.log( '跳转到登录页' )
  29.         }

  30.     }     

  31.     lmStoreChange(){
  32.         this.m++
  33.         console.log(this.m)
  34.         console.log( '进入了lmStoreChange 方法')

  35.         // 接收所有数据,然后进行过滤,取出新闻的部分
  36.         let allStore = store.getState();
  37.         this.setState( allStore );

  38.     }
  39.     // 改变值
  40.     change(){
  41.         store.dispatch( funGetNewsList() );
  42.         console.log('执行了dispatch方法' )

  43.         // 第一次点击时,先执行了 dispatch 未执行subscribe
  44.         // 第二次点击时, 先执行了subscribe ,再执行 change方法
  45.         
  46.         //  最后一步订阅
  47.         // store.subscribe( this.lmStoreChange ) ;
  48.         // store.subscribe( this.lmStoreChange() );  这样有效果 但是报错
  49.         // store.subscribe(()=>{
  50.         //     console.log('进入了subscribe方法')
  51.         //     this.lmStoreChange()
  52.         // })

  53.     }

  54.     componentWillUnmount = () => {
  55.         this.setState = (state, callback) => {
  56.             return;
  57.         };
  58.     }

  59.     render(){
  60.         return(
  61.             <View >
  62.                 这是商品界面 { this.state.name }
  63.                
  64.                 <View onTap={ this.change } > 点我变 </View>

  65.             </View>
  66.         );
  67.     }

  68. }

  69. export default Shop;
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表