欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue?請求后端數(shù)據(jù)的示例代碼

 更新時間:2022年09月09日 09:34:45   作者:星悅糖  
在vue中,我們如何通過請求接口來訪問后端的數(shù)據(jù)呢?在這里簡單總結了一個小示例,對vue請求后端數(shù)據(jù)實例代碼感興趣的朋友一起看看吧

在vue中,我們如何通過請求接口來訪問后端的數(shù)據(jù)呢?在這里簡單總結了一個小示例:

主要問題:如果不封裝的話,在每次請求的時候都要書寫一遍下面的代碼,造成代碼冗余。

1、在src目錄下創(chuàng)建一個utils文件夾,然后在里面創(chuàng)建一個js文件。這里我創(chuàng)建了一個request.js文件。

/*引入axios*/
import axios from 'axios'
const request = axios.create({
    baseURL: 'http://localhost:8280/user', // 基礎路徑,將統(tǒng)一的部分全部封裝
    withCredentials: true // 表示請求可以攜帶cookie
})
//前端采用export.default,在寫后端代碼時用module.export
export default request

在app.vue中進行測試:

<script>
import request from './utils/request'
 
export default {
  created() {
    request({
      method:'GET',
      url:'/products',
      params:{test:'111',hello:'world'},
    })
  },
}
</script>

2、在src文件夾下創(chuàng)建一個api文件夾,根據(jù)不同的功能進行分組,分別寫不同的接口。這里我創(chuàng)建了一個product.js。

import request from '../utils/request';
 
export function getList(params={}) {
    return request({
        methods:'GET',
        url:'/products',
        params,
    })
}
 
export function getProduct(id) {
    return request({
        methods:'GET',
        url:'/products/${id}',
    })
}
export function update(id,data) {
    return request({
        methods:'PUT',
        url:'/products/${id}',
        data,
    })
}

3、在api文件夾下創(chuàng)建index.js

import products from './products';
 
export default{
    products,
}

4、在main.js中引入api文件夾下的index。

import api from './api/index.js';
 
Vue.prototype.$api = api

5、此時通過接口獲取后端數(shù)據(jù)的方式就變成了如下格式:

getProducts(){
    this.$api.products.getList(this.query).then((response)=>{
        this.products = response.data.data
        this.total = response.data.total
    })
}

6、列表展示案例:

main.js中添加代碼

import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios';
 
//Vue.prototype.$http=axios;//修改內部的$http為axios  $http.get("") .post()
 
Vue.config.productionTip = false
 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  render:h => h(App),
  beforeCreate() {
    //安裝全局事件總線,$bus就是當前應用的vm
    Vue.prototype.$bus = this
  },
 
})

App.vue中添加代碼

<template>
  <div>
    <Search/>
    <List/>
  </div>
</template>
 
<script>
import List from "./components/List";
import Search from "./components/Search";
export default {
  name: 'App',
  components: {Search, List},
}
</script>
 
<style>
 
</style>

router下的index.js中的代碼

import Vue from 'vue'
import Router from 'vue-router'
 
Vue.use(Router)
 
export default new Router({
  routes: [
 
  ]
})

list.vue代碼示例:

<template>
  <div class="row">
    <!--展示用戶數(shù)據(jù)-->
    <div class="card" v-show="info.users.length" v-for="user in info.users" :key="user.login">
      <a :href="user.html_url" target="_blank">
        <img :src="user.avatar_url" style="width: 100px"/>
      </a>
      <p class="card-text">{{user.login}}</p>
    </div>
    <!--展示歡迎詞-->
    <h1 v-show="info.isFirst">歡迎使用</h1>
    <!--展示加載中-->
    <h1 v-show="info.isLoading">加載中....</h1>
    <!--展示錯誤信息-->
    <h1 v-show="info.errMsg">{{info.errMsg}}</h1>
  </div>
</template>
 
<script>
export default {
  name: "List",
  data(){
    return{
     info:{
       isFirst:true,//是否是初次展示
       isLoading:false,//是否處于加載中
       errMsg:'',
       users:[],
     }
    }
  },
  //使用全局事件總線在兩個組件之間傳遞數(shù)據(jù)
  //接收數(shù)據(jù):list組件想接收數(shù)據(jù),則要在list組件中給$bus綁定自定義事件,事件的回調留在list組件自身。
  mounted() {
    this.$bus.$on('updateListDate',(dataObj)=>{
      console.log(dataObj)
      this.info = {...this.info,...dataObj};
      /*this.isFirst = isFirst
      this.isLoading = isLoading
      this.errMsg = errMsg
      this.users = users*/
    })
  },
}
</script>
 
<style scoped>
 
</style>

search.vue代碼示例:

<template>
  <section class="jumbotron">
    <h3 class="jumbotron-heading"> search gitHub Users</h3>
    <div>
      <input type="text" placeholder="enter the name you search" v-model="keyWord"/>&nbsp;
      <button @click="searchUsers" >Search</button>
    </div>
  </section>
</template>
<script>
import axios from 'axios';
export default {
  name: "Search",
  data(){
    return{
      keyWord:''
    }
  },
  methods:{
    searchUsers(){
      //請求前更新list里面的數(shù)據(jù)
      this.$bus.$emit('updateListDate',{isFirst:false,isLoading:true,errMsg:'',users:[]})
      axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
        res =>{
          console.log("請求成功")
          //提供數(shù)據(jù):search組件要給list組件傳遞數(shù)據(jù),就要觸發(fā)list組件中的自定義事件并攜帶要傳遞的數(shù)據(jù)
          //請求成功后更新list里面的數(shù)據(jù)
          this.$bus.$emit("updateListDate",{isLoading:false,errMsg:'',users:res.data.items})
        },
        error =>{
          console.log("請求成功",error.message)
          //請求失敗后更新list里面的數(shù)據(jù)
          this.$bus.$emit("updateListDate",{isLoading:false,errMsg:error.message,users:[]})
        }
      )
    }
  },
}
</script>
 
<style scoped>
 
</style>
 

注:Vue全局事件總線$bus安裝與應用【附帶圖片講解】可以參考下面的地址:

Vue全局事件總線$bus安裝與應用【附帶圖片講解

到此這篇關于vue請求后端數(shù)據(jù)的文章就介紹到這了,更多相關vue請求后端數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue打包后出現(xiàn)空白頁的原因及解決方式詳解

    vue打包后出現(xiàn)空白頁的原因及解決方式詳解

    在項目中很多時候需要用到vue打包成html不需要放在服務器上就能瀏覽,根據(jù)官網打包出來的html直接打開是顯示空白,下面這篇文章主要給大家介紹了關于vue打包后出現(xiàn)空白頁的原因及解決方式的相關資料,需要的朋友可以參考下
    2022-07-07
  • vue如何給自定義的組件綁定點擊事件

    vue如何給自定義的組件綁定點擊事件

    這篇文章主要介紹了vue如何給自定義的組件綁定點擊事件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3輸入單號和張數(shù)如何自動生成連號的單號

    vue3輸入單號和張數(shù)如何自動生成連號的單號

    最近遇到這樣的需求輸入連號事件,需要在表格中輸入物流單號,物流號碼,生成的數(shù)量,名稱,點擊確定自動生成固定數(shù)量的連號物流單號,本文重點介紹vue3輸入單號和張數(shù),自動生成連號的單號,感興趣的朋友一起看看吧
    2024-02-02
  • Vue實現(xiàn)鼠標經過文字顯示懸浮框效果的示例代碼

    Vue實現(xiàn)鼠標經過文字顯示懸浮框效果的示例代碼

    這篇文章主要介紹了Vue實現(xiàn)鼠標經過文字顯示懸浮框效果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 對vue事件的延遲執(zhí)行實例講解

    對vue事件的延遲執(zhí)行實例講解

    今天小編就為大家分享一篇對vue事件的延遲執(zhí)行實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • vue中$emit傳遞多個參(arguments和$event)

    vue中$emit傳遞多個參(arguments和$event)

    本文主要介紹了vue中$emit傳遞多個參(arguments和$event),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Vue?router?路由守衛(wèi)詳解

    Vue?router?路由守衛(wèi)詳解

    這篇文章主要為大家介紹了Vue?router?路由守衛(wèi),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Mint UI實現(xiàn)A-Z字母排序的城市選擇列表

    Mint UI實現(xiàn)A-Z字母排序的城市選擇列表

    這篇文章主要為大家詳細介紹了Mint UI實現(xiàn)A-Z字母排序的城市選擇列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Vue resource中的GET與POST請求的實例代碼

    Vue resource中的GET與POST請求的實例代碼

    本篇文章主要介紹了Vue resource中的GET與POST請求的實例代碼,非常具有實用價值,需要的朋友可以參考下
    2017-07-07
  • Vue編譯器源碼分析compileToFunctions作用詳解

    Vue編譯器源碼分析compileToFunctions作用詳解

    這篇文章主要為大家介紹了Vue編譯器源碼分析compileToFunctions作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07

最新評論