HAP_Vue实现前后端分离(调用hap后台接口)

项目需求:
  实现前后端分离:vue+后端接口(hap)
  • vue.js安装

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

测试命令:
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

去到一个目录下

vue init webpack 项目name

安装项目所需的依赖包资源

cnpm install

运行项目

npm run dev
  • idea开发项目


    13265172-ec0b1372b10ef92b.png
    image.png
  • vue获取接口数据
<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>

13265172-d274ce5653ac6e96.png
image.png

备注:

问题一:
13265172-495827c721647959.png
image.png
问题二:

13265172-0e349b1c8359f86d.png
image.png

解决:执行: npm install axios
13265172-b10bf1ecbf590788.png
image.png

问题三:未引入

<script src="../../node_modules/vue/dist/vue.js"/>
<script src="../../node_modules/vue-resource/dist/vue-resource.js"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值