[Vue-rx] Stream an API using RxJS into a Vue.js Template

本教程介绍如何使用Vue.js结合RxJS从远程服务器获取数据并直接映射到模板中显示。通过axios和vue-axios库发起HTTP请求,并利用RxJS的Observable来处理响应数据,最终展示为Vue组件内的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You can map remote data directly into your Vue.js templates using RxJS. This lesson uses axios (and the vue-axios library which exposes axios on components as this.$http) and wraps the remote call inside of an Observable to provide the data into the template

 

Install:

npm i --save axios vue-axios

 

main.js:

import 'buefy/lib/buefy.css'

import Vue from 'vue'
import App from './App.vue'

import Rx from 'rxjs'
import VueRx from 'vue-rx'

import Buefy from 'buefy'

import Axios from 'axios';
import VueAxios from 'vue-axios'

Vue.use(VueRx, Rx)
Vue.use(Buefy)
Vue.use(VueAxios, Axios)

Vue.config.productionTip = false

new Vue({
  render: h => h(App)
}).$mount('#app')

 

app.vue:

<template>
  <section class="section">
    <h2>
      {{people$}}
    </h2>
  </section>
</template>

<script>
import {  from } from 'rxjs';
import { pluck } from 'rxjs/operators';

export default {
  name: 'app',
  subscriptions() {

    const people$ = from(
      this.$http.get(
        "https://starwars.egghead.training/people/1"
      )
    ).pipe(
      pluck('data', 'name') // Get response.data.name
    )return {
      people$
    }
  }
};
</script>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值