HAP_Vue實(shí)現(xiàn)前后端分離(調(diào)用hap后臺(tái)接口)

項(xiàng)目需求:
  實(shí)現(xiàn)前后端分離:vue+后端接口(hap)
  • vue.js安裝

下載:https://nodejs.org/en/

測(cè)試命令:
node -v
npm -v

輸入以下命令npm -g install npm,更新npm至最新版本。

npm -g install npm

安裝cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

cnpm安裝腳手架vue-cli

cnpm install -g vue-cli

去到一個(gè)目錄下

vue init webpack 項(xiàng)目name

安裝項(xiàng)目所需的依賴包資源

cnpm install

運(yùn)行項(xiàng)目

npm run dev
  • idea開(kāi)發(fā)項(xiàng)目


    image.png
  • vue獲取接口數(shù)據(jù)
<script src="../../node_modules/vue/dist/vue.js"/>
<script src="../../node_modules/vue-resource/dist/vue-resource.js"/>
<template>
 <h1>hh</h1>
</template>

<script>
import Vue from 'vue'
import axios from 'axios'

var vueResource = require('vue-resource')
Vue.use(vueResource)

export default {
  data () {
    return {
      news: [],
      error: ''
    }
  },

  created:
      function () {
        this.getData()
      },

  methods: {
    // getDate: function () {
    //   this.$nextTick(() => {
    //     this.$.ajax({
    //       type: 'get',
    //       url: 'http://localhost:8080/api/public/xxinv/outstorage/trx/querytest',
    //       data: { user_id: 1 },
    //       success: function (data) {
    //         alert(data)
    //         console.log(data)
    //       }
    //     })
    //   })
    // }
    getData: function () {
      const that=new Vue
      axios.get('http://localhost:8080/api/public/xxinv/outstorage/trx/querytest').then((response) => {
       // that.$set('news', response.body.rows)
        alert(response.body)
        console.log(response.data.rows[0].organizationName)
      }, (response) => {
        this.error = response.error_code
      })
    }
  }
}
</script>

image.png

備注:

問(wèn)題一:
image.png
問(wèn)題二:

image.png

解決:執(zhí)行:npm install axios
image.png

問(wèn)題三:未引入

<script src="../../node_modules/vue/dist/vue.js"/>
<script src="../../node_modules/vue-resource/dist/vue-resource.js"/>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容