vue简单请求后端接口渲染到页面

一、创建一个新的vue项目

  1. 安装node环境(推荐:Vue环境搭建+VSCode+Win10)
  2. 终端执行命令(创建一个新的项目): vue init webpack ”项目名称“
  3. cnpm install 打包项目
  4. cnpm run dev 启动项目
  5. 初始项目已经建好啦…

二、修改HellowWorld.vue代码如下

<template>
  <div id="app">
    <table type="selection" align="center" border="1">
      <tr>
        <td>姓名</td>
        <td>年龄</td>
      </tr>
      <!-- 请求参数请按照自己返回数据解析-->
      <div v-for="item in info.data" :key='item'>
        <tr>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </div>
    </table>
  </div>
</template>

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

export default {
  data () {
    return {
      info: null
    }
  },
  mounted () {
    axios
      .get('/api/user/findUser')// 若没有更改index.js配置此处应写全部路径 http://localhost:6062/user/findUser
      .then(response => (this.info = response.data),
        reason => {
          console.log('error')
        })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

三、保存项目自动加载查看前台页面

运行结果
在这里插入图片描述

四、本地测试如果遇到跨域问题解决办法

在config文件夹下面的index.js修改配置如下
修改前:

在这里插入图片描述
修改后:
在这里插入图片描述

贴上代码:

module.exports = {
  dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:6062',
        changeOrigin: true,
        pathRewrite: {
          '/api': ''
        }
      }
    },

至此,应该没啥问题了,刚刚开始接触vue,若有问题请不吝指教!

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Benzi啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值