亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue-rx的初步使用教程

 更新時(shí)間:2018年09月21日 14:18:06   作者:水痕01  
這篇文章主要介紹了vue-rx的初步使用教程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一、各文檔介紹

1、rxjs官網(wǎng)

2、vue-rxjs地址

二、環(huán)境搭建

1、使用vue-cli構(gòu)建一個(gè)項(xiàng)目

2、安裝vue-rx的依賴包

yarn add rxjs
yarn add rxjs-compat
yarn add vue-rx

3、在src/main.js中配置使用rxjs

// 使用vueRx
import VueRx from 'vue-rx';
import Rx from 'rxjs/Rx'

Vue.use(VueRx, Rx);

三、沒(méi)有使用vue-rx的時(shí)候

1、關(guān)于屬性的使用

import { Observable } from 'rxjs';
export default {
 data() {
  return {
   name: new Observable.of('張三')
  }
 }
};

2、關(guān)于事件的使用

import { Observable } from 'rxjs';
export default {
 data() {
  return {
   name: new Observable.of('張三'),
  }
 },
 mounted () {
  new Observable.fromEvent(this.$refs.btn, 'click').subscribe(e => {
   this.name = '哈哈';
  })
 }
};

四、結(jié)合vue-rx的使用

1、項(xiàng)目中集成了vue-rx的時(shí)候會(huì)在vue中新增一個(gè)鉤子函數(shù)subscriptions,和之前的data類似的使用

2、domStreams是一個(gè)數(shù)組用來(lái)存放事件

3、屬性的使用

export default {
 name: 'HelloWorld',
 data () {
  return {
   msg: 'Welcome to Your Vue.js App'
  }
 },
 domStreams: ['setName$', 'resetMsg$'],
 subscriptions() {
  return {
   // 發(fā)送一個(gè)普通的值
   name: new Observable.of('張三'),
   // 發(fā)送一個(gè)修改的值
   age$ : Observable.of(20).map(item => item + 10),
   // 定義發(fā)送一個(gè)數(shù)組
   arr$: new Observable.of(['第一本書', '第二本書']),
   // 發(fā)送一個(gè)數(shù)組
   obj$: new Observable.of({ 
    a: 'test-obj',
    name: '呵呵' 
   }),
   // 發(fā)送一個(gè)promise函數(shù)
   promise$: new Observable.fromPromise(this.getPromise()),
   // 定時(shí)器
   interval$: new Observable.interval(1000)
  }
 },
 methods: {
  getPromise() {
   return new Promise((resolve, reject) => {
    setTimeout(() => {
     resolve('promise');
    }, 1000)
   })
  }
 },
}

5、事件的使用

1.在視圖中定義事件

<button v-stream:click="setName$">點(diǎn)擊按鈕設(shè)置值</button>

2.在domStreams中定義

domStreams: ['setName$'],

3、在subscriptions定義事件賦值給哪個(gè)變量

name$: this.setName$.map(e => 'hello'.toUpperCase()),

六、關(guān)于switchMap、concatMap、exhaustMap的使用

1、定義事件

<button class="btn" v-stream:click="getConcatMapCount$">點(diǎn)擊獲取concatMapCount$</button>
<p>{{concatMapCount$}}</p>
<button class="btn" v-stream:click="getSwitchMapCount$">點(diǎn)擊獲取switchMapCount$</button>
<p>{{switchMapCount$}}</p>
<button class="btn" v-stream:click="getExhaustMapCount$">點(diǎn)擊獲取exhaustMapCount$</button>
<p>{{exhaustMapCount$}}</p>

2、定義事件名

domStreams: ['getConcatMapCount$', 'getSwitchMapCount$', 'getExhaustMapCount$'],

3、觸發(fā)事件

import { Observable } from 'rxjs';
export default {
 data() {
  return {
   count: 0,
  }
 },
 domStreams: ['getConcatMapCount$', 'getSwitchMapCount$', 'getExhaustMapCount$'],
 subscriptions() {
  return {
   // 當(dāng)你連續(xù)點(diǎn)擊按鈕多次獲取數(shù)據(jù)時(shí),cancatMap會(huì)將獲取到的數(shù)據(jù)按隊(duì)列發(fā)出
   concatMapCount$: this.getConcatMapCount$.concatMap(e => {
    return new Observable.from(this.getCount());
   }),
   // 當(dāng)你連續(xù)點(diǎn)擊按鈕多次獲取數(shù)據(jù)時(shí),switchMap只會(huì)將最后一個(gè)點(diǎn)擊發(fā)出的值發(fā)出,前面發(fā)出的值會(huì)被吞掉
   switchMapCount$: this.getSwitchMapCount$.switchMap(e => {
    return new Observable.from(this.getCount());
   }),
   // 當(dāng)你連續(xù)點(diǎn)擊按鈕多次時(shí),exhaustMap僅執(zhí)行一次,在第一次值發(fā)出后,才可以繼續(xù)點(diǎn)擊下一次發(fā)出值
   exhaustMapCount$: this.getExhaustMapCount$.exhaustMap(e => {
    return new Observable.from(this.getCount())
   })
  }
 },
 methods: {
  getCount() {
   return new Promise((resolve, reject) => {
    this.count ++;
    setTimeout(() => {
     resolve(this.count);
    }, 1000);
   })
  }
 }
};

七、事件中傳遞參數(shù)

1、html頁(yè)面

<ul>
 <li v-for="(num, index) in numList" :key="index" v-stream:click="{
  subject: getNum$,
  data: {
   'index': index,
   'num': num
  }
 }">{{ num }}</li>
</ul>
<p>點(diǎn)擊的數(shù)字為:{{ num$.index }}</p>
<p>點(diǎn)擊的數(shù)字下標(biāo)為:{{ num$.num }}</p>

2、在vue中處理

import { Observable } from 'rxjs'
export default {
 data () {
  return {
   numList: [1, 2, 3]
  }
 },
 // v-stream事件可以統(tǒng)一寫在這里,具體可以看vue-rx的使用
 domStreams: [
  'getNum$'
 ],
 subscriptions () {
  return {
   num$: this.getNum$
    // 從傳入的對(duì)象中獲取key為data的value,傳入下一個(gè)operator
    .pluck('data')
    .map(data => data)
    // 因?yàn)橐晥D引用了num$.index,所以這里要初始化num$為對(duì)象,避免報(bào)錯(cuò)
    .startWith({})
  }
 }
}

3、輸入框中獲取值

<input type="text" v-stream:keyup="getInput$">
<p>value$: {{ value$ }}</p>
import { Observable } from 'rxjs';
export default {
 domStreams: ['getInput$'],
 subscriptions () {
  return {
   value$: this.getInput$
    // 選取e.event.target.value
    .pluck('event', 'target', 'value')
    .debounceTime(2000)
    // 根據(jù)value值作比較,如果和上一次一樣則不發(fā)出值
    .distinctUntilChanged()
    .map(val => {
     console.log(val);
     return val;
    })
  }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論