最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

在vue中,我們?nèi)绾瓮ㄟ^請求接口來訪問后端的數(shù)據(jù)呢?在這里簡單總結(jié)了一個小示例:

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

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', // 基礎(chǔ)路徑,將統(tǒng)一的部分全部封裝
    withCredentials: true // 表示請求可以攜帶cookie
})
//前端采用export.default,在寫后端代碼時用module.export
export default request

在app.vue中進(jìn)行測試:

<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ù)不同的功能進(jìn)行分組,分別寫不同的接口。這里我創(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;//修改內(nèi)部的$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就是當(dāng)前應(yīng)用的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綁定自定義事件,事件的回調(diào)留在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安裝與應(yīng)用【附帶圖片講解】可以參考下面的地址:

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

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

相關(guān)文章

  • Vue?router?路由安裝及使用過程

    Vue?router?路由安裝及使用過程

    vue-router 是 Vue 的一個插件庫,適用于構(gòu)建單頁面應(yīng)用,這篇文章主要介紹了Vue?router?路由安裝以及使用,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Vue中@keyup.enter?@v-model.trim的用法小結(jié)

    Vue中@keyup.enter?@v-model.trim的用法小結(jié)

    這篇文章主要介紹了Vue中@keyup.enter?@v-model.trim的用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-12-12
  • vue 更改連接后臺的api示例

    vue 更改連接后臺的api示例

    今天小編就為大家分享一篇vue 更改連接后臺的api示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 在線使用iconfont字體圖標(biāo)的簡單實(shí)現(xiàn)

    在線使用iconfont字體圖標(biāo)的簡單實(shí)現(xiàn)

    這篇文章主要介紹了在線使用iconfont字體圖標(biāo)的簡單實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue.js使用watch監(jiān)聽路由變化的方法

    vue.js使用watch監(jiān)聽路由變化的方法

    這篇文章主要介紹了vue.js使用watch監(jiān)聽路由變化的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Vue3使用transition實(shí)現(xiàn)組件切換的過渡效果

    Vue3使用transition實(shí)現(xiàn)組件切換的過渡效果

    <Transition> 是一個內(nèi)置組件,這意味著它在任意別的組件中都可以被使用,無需注冊,它可以將進(jìn)入和離開動畫應(yīng)用到通過默認(rèn)插槽傳遞給它的元素或組件上,本文介紹了Vue3使用transition實(shí)現(xiàn)組件切換的過渡效果,需要的朋友可以參考下
    2024-09-09
  • Vue使用vue-area-linkage實(shí)現(xiàn)地址三級聯(lián)動效果的示例

    Vue使用vue-area-linkage實(shí)現(xiàn)地址三級聯(lián)動效果的示例

    很多時候我們需要使用地址三級聯(lián)動,即省市區(qū)三級聯(lián)動,這篇文章主要介紹了Vue使用vue-area-linkage實(shí)現(xiàn)地址三級聯(lián)動效果的示例,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Vue3中使用qrcode庫實(shí)現(xiàn)二維碼生成

    Vue3中使用qrcode庫實(shí)現(xiàn)二維碼生成

    Vue3中實(shí)現(xiàn)二維碼生成需要使用第三方庫來處理生成二維碼的邏輯,常用的庫有?qrcode和?vue-qrcode,本文主要介紹了Vue3中使用qrcode庫實(shí)現(xiàn)二維碼生成,感興趣的可以了解一下
    2023-12-12
  • Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計時效果

    Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計時效果

    這篇文章主要介紹了Vue3實(shí)現(xiàn)獲取驗(yàn)證碼按鈕倒計時效果,用戶點(diǎn)擊獲取驗(yàn)證碼按鈕,發(fā)送請求給后端,按鈕失效,并且開始倒計時60秒;在此期間,用戶無法再次點(diǎn)擊按鈕,即使用戶刷新頁面,倒計時依然存在,直到倒計時完畢,按鈕恢復(fù),感興趣的小伙伴跟著小編一起來看看吧
    2024-10-10
  • Vue父子組件傳值&自定義事件方式

    Vue父子組件傳值&自定義事件方式

    這篇文章主要介紹了Vue父子組件傳值&自定義事件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論

津南区| 阿坝| 东丰县| 正定县| 伊宁县| 西吉县| 桂阳县| 无棣县| 高安市| 长乐市| 于都县| 满城县| 靖边县| 墨竹工卡县| 赤壁市| 霸州市| 莫力| 邯郸市| 屏东县| 黎城县| 金乡县| 台中县| 武邑县| 墨玉县| 佛冈县| 北辰区| 公安县| 望江县| 天等县| 沧源| 互助| 大洼县| 平泉县| 沾益县| 定边县| 通渭县| 随州市| 虎林市| 交口县| 镇平县| 陕西省|