npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wets-mobx

v1.0.20

Published

在小程序中使用Mobx > 提供了5个接口: **Provider、inject、observer、route、autorun**,其中route用于在store中结合action方便的发送HTTP请求,详情请见下面的**getTopicList 方法**,测试代码在test文件夹下面

Downloads

1

Readme

@mtfe/wets-mobx

在小程序中使用Mobx

提供了5个接口: Provider、inject、observer、route、autorun,其中route用于在store中结合action方便的发送HTTP请求,详情请见下面的getTopicList 方法,测试代码在test文件夹下面

# 安装Redux支持包
$ yarn add --dev @mtfe/wets-mobx mobx

# 创建redux的modules目录
$ mkdir -p src/stores

创建 src/stores/index.ts 文件

import { useStrict } from 'mobx';
import Test from './test';

useStrict(true);
const test = new Test();
const store = {
  test,
};

export default store;

创建 src/stores/test.ts 文件

import {
  observable,
  action,
  computed,
  runInAction,
  IObservableArray
} from 'mobx';
import { route } from '@mtfe/wets-mobx';

export type Topic = {
  title: string;
};

export default class Person {
  @observable random = Math.random();
  @observable topicList = [{
    title: '没有主题'
  }] as IObservableArray<Topic>;

  @computed
  get fullRandom() {
    return '帅帅的' + this.random;
  }  

  @action
  changeRandom() {
    this.random = Math.random();
  }

  @action
  @route('/api/v1/topics', 'GET')
  * getTopicList(tab: string) {
    const query = {
      page: 1,
      limit: 10,
      tab
    };
    const ret = yield query;
    const topicList: Topic[] = ret.data;
    runInAction(() =>
      this.topicList.replace(
        topicList.map(item => ({
          title: item.title
        }))
      )
    );
  }
}

修改 app.ts

import { App } from '@mtfe/wets';
import { Provider } from '@mtfe/wets-mobx';
import { route } from '@mtfe/wets-mobx';

import store from '../src/stores';

route.prototype.setUrlOption({
  protocol: 'https',
  host: 'cnodejs.org'
});

@App.Conf({
  window: {
    navigationBarBackgroundColor: '#fff',
    navigationBarTextStyle: 'black',
    backgroundColor: '#fff',
  },
})
@Provider({
  store,
})
export class MyApp extends App {
  store: any;
}

这样就配置完了,接下来就可以像在 React 中使用 mobx 一样了 创建 src/pages/test/index.tsx 文件

import { Page } from '@mtfe/wets';
import { inject, observer, autorun } from '@mtfe/wets-mobx';

import './test.page.css';
import TestStore from '../../stores/test';

interface IData {
  test: TestStore;
  fullRandom: string;
  tabIndex: number;
  tabArray: string[];
}

@Page.Conf({
  navigationBarTitleText: 'TestPage'
})
@inject('test')
@observer()
export class TestPage extends Page<any, IData> {
  onLoad() {
    console.log(this.data);
    this.setData({
      tabIndex: 0,
      tabArray: ['ask', 'share', 'job', 'good']
    });
    autorun(() =>
      this.setData({
        fullRandom: this.data.test.fullRandom
      })
    );
  }

  test() {
    this.data.test.changeRandom();
  }

  getTopicList() {
    this.data.test.getTopicList(this.data.tabArray[this.data.tabIndex]);
  }

  bindPickerChange(e: any) {
    console.log('picker发送选择改变,携带值为', e.detail.value);
    this.setData({
      tabIndex: e.detail.value
    });
  }

  render() {
    return (
      <view className='test-page'>
        <text>{this.data.fullRandom}</text>
        <button bindtap={this.test}>changeRandom</button>
        <view className='section'>
          <view className='section__title'>tab选择器</view>
          <picker
            mode='selector'
            bindchange={this.bindPickerChange}
            value={this.data.tabIndex}
            range={this.data.tabArray}
          >
            <view className='picker'>
              当前选择: {this.data.tabArray[this.data.tabIndex]}
            </view>
          </picker>
        </view>
        <button bindtap={this.getTopicList}>get topic list</button>
        <view className='topicList'>
          {this.data.test.topicList.map((topic, index) => {
            return (
              <view key={index}>
                {index}: {topic.title}
              </view>
            );
          })}
        </view>
      </view>
    );
  }
}