成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

React+TypeScript+Mobx+AntDesignMobile進(jìn)行移動(dòng)端項(xiàng)目搭建

lindroid / 1594人閱讀

摘要:通過(guò)裝飾器或者利用時(shí)調(diào)用的函數(shù)來(lái)進(jìn)行使用下面代碼中當(dāng)或者發(fā)生變化時(shí),會(huì)監(jiān)聽數(shù)據(jù)變化確保通過(guò)觸發(fā)方法自動(dòng)更新。只能影響正在運(yùn)行的函數(shù),而無(wú)法影響當(dāng)前函數(shù)調(diào)用的異步操作參考官方文檔用法裝飾器函數(shù)遵循中標(biāo)準(zhǔn)的綁定規(guī)則。

前言:

本文基于React+TypeScript+Mobx+AntDesignMobile技術(shù)棧,使用Create-React-App腳手架進(jìn)行一個(gè)移動(dòng)端項(xiàng)目搭建,主要介紹項(xiàng)目搭建過(guò)程和相關(guān)配置,以及狀態(tài)管理工具M(jìn)obx的使用,最后實(shí)現(xiàn)點(diǎn)擊按鈕數(shù)字+1和一個(gè)簡(jiǎn)單的TodoList小功能,希望能對(duì)大家有所幫助。GitHub代碼地址

項(xiàng)目搭建具體步驟:

安裝Create-React-App腳手架工具,已經(jīng)安裝的同學(xué)請(qǐng)忽略

npm install create-react-app -g

創(chuàng)建初始項(xiàng)目
create-react-app my-app --typescript

注意:
如果同學(xué)你是參考typescript官方文檔進(jìn)行react項(xiàng)目搭建,里面有用create-react-app my-app --scripts-version=react-scripts-ts命令創(chuàng)建的,千萬(wàn)別用,現(xiàn)在已經(jīng)過(guò)時(shí)了,
webpack版本有問(wèn)題,后續(xù)配置各種想不到的坑 TypeScript中文官網(wǎng)

引入AntDesignMobile,實(shí)現(xiàn)組件按需加載

本步驟官網(wǎng)有比較詳細(xì)的介紹,我就不一一列舉配置過(guò)程了,建議大家不要eject暴露所有內(nèi)建配置,后續(xù)版本升級(jí)維護(hù)可能比較麻煩,推薦使用 react-app-rewired 插件即可配置;AntDesignMobile官網(wǎng)

安裝React路由,狀態(tài)管理工具mobx,配置sass

    npm install history @types/history react-router-dom @types/react-router-dom // 安裝react路由
    npm install mobx-react mobx  // 安裝mobx
    npm install node-sass // 安裝sass工具,安裝之后無(wú)需另外配置sass,腳手架工具已經(jīng)集成了
    

基本配置完成,運(yùn)行項(xiàng)目
npm run start

React狀態(tài)管理工具M(jìn)obx介紹:

Mobx是一個(gè)功能強(qiáng)大,基于面向?qū)ο缶幊谭绞降囊粋€(gè)狀態(tài)管理工具,上手相對(duì)比較容易。就連Redux的作者也曾經(jīng)向大家推薦過(guò)它,對(duì)TypeScript支持比較友好,參考官網(wǎng)一張流程圖:
Mobx中文官網(wǎng)

下面介紹幾個(gè)Mobx核心概念
Observable state(可觀察的狀態(tài))
MobX 為現(xiàn)有的數(shù)據(jù)結(jié)構(gòu)(如對(duì)象,數(shù)組和類實(shí)例)添加了可觀察的功能。 通過(guò)使用 @observable 裝飾器(ES.Next)來(lái)給你的類屬性添加注解就可以簡(jiǎn)單地完成這一切。
import { observable } from "mobx";
class Todo {
    id = Math.random();
    @observable title = "";
    @observable finished = false;
}
Computed values(計(jì)算值)
使用 MobX, 你可以定義在相關(guān)數(shù)據(jù)發(fā)生變化時(shí)自動(dòng)更新的值。 通過(guò)@computed 裝飾器或者利用 (extend)Observable 時(shí)調(diào)用 的getter / setter 函數(shù)來(lái)進(jìn)行使用,下面代碼中當(dāng)queue或者refresh發(fā)生變化時(shí),MobX會(huì)監(jiān)聽數(shù)據(jù)變化確保,通過(guò)Computed觸發(fā)fooProps方法自動(dòng)更新。
import React from "react";
import {observable, isArrayLike, computed, action, autorun, when, reaction,runInAction} from "mobx";
import {observer} from "mobx-react";

// 定義數(shù)據(jù)Store
class Store {
    @observable queue:number = 1;
    @action refresh = ():void => {
        this.queue += 1;
        console.log("this.queue -> ", this.queue);
    }
    @computed get fooProps():any {
        return {
            queue: this.queue,
            refresh: this.refresh
        };
    }
  }
Actions(動(dòng)作)
任何應(yīng)用都有動(dòng)作。動(dòng)作是任何用來(lái)修改狀態(tài)的東西,mobx推薦將修改被觀測(cè)變量的行為放在action中。action只能影響正在運(yùn)行的函數(shù),而無(wú)法影響當(dāng)前函數(shù)調(diào)用的異步操作,參考官方文檔用法:
action(fn)
action(name, fn)
@action classMethod() {}
@action(name) classMethod () {}
@action boundClassMethod = (args) => { body }
@action(name) boundClassMethod = (args) => { body }
@action.bound classMethod() {}
action 裝飾器/函數(shù)遵循 javascript 中標(biāo)準(zhǔn)的綁定規(guī)則。 但是,action.bound 可以用來(lái)自動(dòng)地將動(dòng)作綁定到目標(biāo)對(duì)象。 注意,與 action 不同的是,(@)action.bound 不需要一個(gè)name參數(shù),名稱將始終基于動(dòng)作綁定的屬性。
class Ticker {
    @observable tick = 0

    @action.bound
    increment() {
        this.tick++ // "this" 永遠(yuǎn)都是正確的
    }
}

const ticker = new Ticker()
setInterval(ticker.increment, 1000)
利用Mobx作為狀態(tài)管理,實(shí)現(xiàn)兩個(gè)小功能
點(diǎn)擊按鈕+1
import React from "react";
import {observable, isArrayLike, computed, action, autorun, when, reaction,runInAction} from "mobx";
import {observer} from "mobx-react";
import {Button} from "antd-mobile";
import "./index.scss";

// 定義數(shù)據(jù)Store,用Mobx作為狀態(tài)管理工具
class Store {
    @observable queue:number = 1;
    @action refresh = ():void => {
        this.queue += 1;
        console.log("this.queue -> ", this.queue);
    }
    @computed get fooProps():any {
        return {
            queue: this.queue,
            refresh: this.refresh
        };
    }
  }
// ts組件接收父組件傳遞過(guò)來(lái)的數(shù)據(jù)必須定義接口類型,否則報(bào)錯(cuò)
interface BarProps{
    queue :number
}
// @observer修飾類,Bar組件接受Foo組建傳過(guò)來(lái)的queue屬性
@observer 
class Bar extends React.Component{
    render() {
        const {queue} = this.props
        return (
            
{queue}
) } } interface FooProps { queue: number, refresh():void } // Foo組件接收來(lái)自Add組件的store數(shù)據(jù) @observer class Foo extends React.Component{ render() { const {queue,refresh} = this.props; return (
) } } // 初始化store數(shù)據(jù),傳遞給Foo組件 const store = new Store(); class Add extends React.Component{ render() { return (

hello react-ts-mobx

) } } export default observer(Add)
TodoList功能
import React from "react";
import {observable, isArrayLike, computed, action, autorun, when, reaction,runInAction} from "mobx";
import {observer} from "mobx-react";
import "./index.scss";
// 定義Todo數(shù)據(jù)類型
class Todo {
    id:number = new Date().getTime();
    title:string = "";
    finished:boolean = false;
    constructor(title:string) {
        this.title = title;
    }
}
// Store數(shù)據(jù)方法管理
class Store {
    @observable title:string = "";
    @observable todos:Todo[] =[];
    // 添加Todo的Title
    @action createTitle (e:any) {
        console.log("e",e.target.value);
        this.title = e.target.value;
    }
    // 增加Todo數(shù)據(jù)
    @action createTodo = () => {
        this.todos.unshift(new Todo(this.title));
        this.title = "";
    }
    // 刪除Todo
    @action delTodo (id:number) {
        this.todos.forEach((item,index) => {
            if (item.id === id) {
                this.todos.splice(index,1)
            }
        })
    }
    // 監(jiān)聽todos數(shù)據(jù)變化,顯示剩余待辦數(shù)量
    @computed get unfinished () {
        return this.todos.filter(todo => !todo.finished).length;
    }

}
interface TodoItemProps {
    todo:any;
    store:any;
}
// 每一條Todo數(shù)據(jù)組件
@observer 
class TodoItem extends React.Component {
    render() {
        const {todo,store} = this.props
        return (
            
{todo.title} store.delTodo(todo.id)}>X
) } } const store = new Store(); @observer class TodoList extends React.Component { render() { return (

TodoList

store.createTitle(e)} />
    {store.todos.map((todo)=>{ return
  • })}
{store.unfinished} item(s) unfinished
) } } export default TodoList
總結(jié):

本人剛接觸TypeScript和Mobx不久,總結(jié)學(xué)習(xí)方法:應(yīng)該先熟悉一些基本概念后,慢慢的做一些小功能,遇到問(wèn)題認(rèn)真思考后再查資料或者請(qǐng)教別人,反復(fù)看文檔,加強(qiáng)對(duì)知識(shí)的理解;歡迎大佬們留言交流;GitHub代碼地址

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/105184.html

相關(guān)文章

  • React結(jié)合TypeScriptMobx初體驗(yàn)

    摘要:結(jié)合編輯器可以推導(dǎo)變量對(duì)應(yīng)的類型以及內(nèi)部的結(jié)構(gòu),提高代碼的健壯性和可維護(hù)性。通過(guò)充分利用時(shí)間回溯的特征,可以增強(qiáng)業(yè)務(wù)的可預(yù)測(cè)性與錯(cuò)誤定位能力。對(duì)于對(duì)象的哪部分需要成為可觀察的,提供了細(xì)粒度的控制。 showImg(https://segmentfault.com/img/bVba6Ts?w=1218&h=525); 為什么要使用TypeScript 偵測(cè)錯(cuò)誤 通過(guò)靜態(tài)類型檢測(cè)可以盡早檢...

    dreambei 評(píng)論0 收藏0
  • 每周清單第 45 期: Safari 支持 Service Worker, Parcel 完整教

    摘要:的另一個(gè)核心特性,蘋果表示也正在開發(fā)中,按開發(fā)進(jìn)度可能幾個(gè)月后就能與我們見面。是基于的本地化數(shù)據(jù)庫(kù),支持以及瀏覽器環(huán)境。 前端每周清單專注前端領(lǐng)域內(nèi)容,以對(duì)外文資料的搜集為主,幫助開發(fā)者了解一周前端熱點(diǎn);分為新聞熱點(diǎn)、開發(fā)教程、工程實(shí)踐、深度閱讀、開源項(xiàng)目、巔峰人生等欄目。歡迎關(guān)注【前端之巔】微信公眾號(hào)(ID: frontshow),及時(shí)獲取前端每周清單。 本期是 2017 年的最后一...

    趙春朋 評(píng)論0 收藏0
  • 2019年前的3個(gè)趨勢(shì)

    摘要:組件成為前端最基本的物料,融合在組件中的方案日趨成熟。組件成為最基本的前端物料,讓組件化更徹底在的調(diào)研報(bào)告中,開發(fā)者有愿意繼續(xù),有愿意繼續(xù)。需要留意的是,有表示對(duì)感興趣,因此獲得的最感興趣獎(jiǎng)。 簡(jiǎn)介: JavaScript 應(yīng)用范圍廣泛,靜態(tài)類型語(yǔ)言 TypeScript 會(huì)繼續(xù)得到更多開發(fā)者的青睞。 組件成為前端最基本的物料,CSS 融合在組件中(CSS in JS)的方案日趨成熟...

    yanwei 評(píng)論0 收藏0
  • 有個(gè)功能豐富的 react 腳手架,了解下?

    摘要:前言想要快速開始多頁(yè)面應(yīng)用項(xiàng)目結(jié)構(gòu)如何更合理想要快速上手想要快速使用想要一鍵使用并能快速自定義主題樣式可以的這里,受和的啟發(fā),我做了這樣一個(gè)的腳手架,讓你一鍵搭建項(xiàng)目,快速開始。 前言 想要快速開始 react 多頁(yè)面應(yīng)用? 項(xiàng)目結(jié)構(gòu)如何更合理? 想要快速上手 Mobx? 想要快速使用 TypeScript? 想要一鍵使用 Ant-Design 并能快速自定義主題樣式? 可以的?。?! ...

    senntyou 評(píng)論0 收藏0
  • React全家桶環(huán)境搭建過(guò)程

    摘要:環(huán)境搭建從零開始搭建開發(fā)環(huán)境引入安裝依賴新建修改引入并支持安裝依賴打包時(shí)將樣式模塊化,我們可以通過(guò)或引入樣式,并且相互不沖突。修改,引入創(chuàng)建使用語(yǔ)法報(bào)錯(cuò)修改引入狀態(tài)管理使用裝飾器語(yǔ)法修改修改源碼 環(huán)境搭建 1.從零開始搭建webpack+react開發(fā)環(huán)境 2.引入Typescript 安裝依賴 npm i -S @types/react @types/react-domnpm i -...

    Genng 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<