vue-Element-axios搭建调用api进行数据展示_情愫_前端开发者

1全局安装vue-cli

输入命令:npm install vue-cli -g
输入命令:npm install vue-cli -g

2创建项目框架

输入命令:vue init webpack vueapi
输入命令:vue init webpack vueapi

3依次按照提示输入,项目名、项目描述、项目作者等等,

4.进入vueapi目录

输入命令: cd vueapi
输入命令: cd vueapi

5.安装element-ui

输入命令:npm i element-ui -S
输入命令:npm i element-ui -S

因为是基于Vue.js和elementUI进行的项目开发,所以当然要导入Vue.j包和elementUI包:

npm install --save vue element-ui
npm install --save vue element-ui

6.安装vue-router

要进行页面跳转,所以要用到vue-router:

输入命令:npm install --save vue-router
输入命令:npm install --save vue-router

7.安装axios

要从后端获取数据,所以要ajax请求,用vue官方推荐的axios:

输入命令:npm install --save axios
输入命令:npm install --save axios

启动项目

启动项目

输入:npm run dev
输入:npm run dev

构建vue项目目录大致如下:

buid:构建脚本目录
config:构建配置目录
node_modules:依赖node工具包目录
src:源码目录
assets:资源目录
components:组件目录
router:
App.vue 页面vue组件
main.js:页面入口js文件
static:静态文件目录
test:测试文件目录
.eslintrc.js:es语法检查配置
index.html:入口页面
package.json :项目描述文件
buid:构建脚本目录
config:构建配置目录
node_modules:依赖node工具包目录
src:源码目录
assets:资源目录
components:组件目录
router:
App.vue 页面vue组件
main.js:页面入口js文件
static:静态文件目录
test:测试文件目录
.eslintrc.js:es语法检查配置
index.html:入口页面
package.json :项目描述文件

buid:构建脚本目录
config:构建配置目录
node_modules:依赖node工具包目录
src:源码目录
assets:资源目录
components:组件目录
router:
App.vue 页面vue组件
main.js:页面入口js文件
static:静态文件目录
test:测试文件目录
.eslintrc.js:es语法检查配置
index.html:入口页面
package.json :项目描述文件

 

 调用后台接口:需要引入axios

在main.js 引入

import axios from 'axios'
Vue.prototype.$http = axios
Vue.prototype.$http.defaults.baseURL = '' // `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL

import axios from 'axios'
Vue.prototype.$http = axios
Vue.prototype.$http.defaults.baseURL = '' // `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL

fromaxios
Vue.prototype.$http
axios
Vue.prototype.$http.defaults.baseURL
// `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL

 

如下图(编辑前端页面)

<template>
  <el-container style="height: 680px; border: 10px solid #eee">
    <el-aside width="250px" style="background-color: rgb(238, 241, 246)">
      <el-menu :default-openeds="['1', '3']">
        <el-submenu index="1">
          <template slot="title"
            ><i class="el-icon-message"></i>导航一</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="1-1">选项1</el-menu-item>
            <el-menu-item index="1-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="1-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="1-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="1-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="2">
          <template slot="title"
            ><i class="el-icon-menu"></i>导航二</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="2-1">选项1</el-menu-item>
            <el-menu-item index="2-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="2-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="2-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="2-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="3">
          <template slot="title"
            ><i class="el-icon-setting"></i>导航三</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="3-1">选项1</el-menu-item>
            <el-menu-item index="3-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="3-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="3-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="3-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span>王小虎</span>
      </el-header>

      <el-main>
        <el-table :data="list">
          <el-table-column prop="datatext" label="日期" width="140">
          </el-table-column>
          <el-table-column prop="version" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="id" label="地址"> </el-table-column>
          <el-table-column prop="text" label="描述"> </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </el-container>
</template>

template

<template>
  <el-container style="height: 680px; border: 10px solid #eee">
    <el-aside width="250px" style="background-color: rgb(238, 241, 246)">
      <el-menu :default-openeds="['1', '3']">
        <el-submenu index="1">
          <template slot="title"
            ><i class="el-icon-message"></i>导航一</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="1-1">选项1</el-menu-item>
            <el-menu-item index="1-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="1-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="1-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="1-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="2">
          <template slot="title"
            ><i class="el-icon-menu"></i>导航二</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="2-1">选项1</el-menu-item>
            <el-menu-item index="2-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="2-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="2-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="2-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="3">
          <template slot="title"
            ><i class="el-icon-setting"></i>导航三</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="3-1">选项1</el-menu-item>
            <el-menu-item index="3-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="3-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="3-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="3-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span>王小虎</span>
      </el-header>

      <el-main>
        <el-table :data="list">
          <el-table-column prop="datatext" label="日期" width="140">
          </el-table-column>
          <el-table-column prop="version" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="id" label="地址"> </el-table-column>
          <el-table-column prop="text" label="描述"> </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </el-container>
</template>
<template>
  <el-container style="height: 680px; border: 10px solid #eee">
    <el-aside width="250px" style="background-color: rgb(238, 241, 246)">
      <el-menu :default-openeds="['1', '3']">
        <el-submenu index="1">
          <template slot="title"
            ><i class="el-icon-message"></i>导航一</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="1-1">选项1</el-menu-item>
            <el-menu-item index="1-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="1-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="1-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="1-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="2">
          <template slot="title"
            ><i class="el-icon-menu"></i>导航二</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="2-1">选项1</el-menu-item>
            <el-menu-item index="2-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="2-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="2-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="2-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
        <el-submenu index="3">
          <template slot="title"
            ><i class="el-icon-setting"></i>导航三</template
          >
          <el-menu-item-group>
            <template slot="title"
              >分组一</template
            >
            <el-menu-item index="3-1">选项1</el-menu-item>
            <el-menu-item index="3-2">选项2</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="分组2">
            <el-menu-item index="3-3">选项3</el-menu-item>
          </el-menu-item-group>
          <el-submenu index="3-4">
            <template slot="title"
              >选项4</template
            >
            <el-menu-item index="3-4-1">选项4-1</el-menu-item>
          </el-submenu>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span>王小虎</span>
      </el-header>

      <el-main>
        <el-table :data="list">
          <el-table-column prop="datatext" label="日期" width="140">
          </el-table-column>
          <el-table-column prop="version" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="id" label="地址"> </el-table-column>
          <el-table-column prop="text" label="描述"> </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </el-container>
</template>

<template><el-container style=”height: 680px; border: 10px solid #eee”><el-aside width=”250px” style=”background-color: rgb(238, 241, 246)”><el-menu :default-openeds=”[‘1’, ‘3’]”><el-submenu index=”1″><template slot=”title”><i class=”el-icon-message”></i></template
><el-menu-item-group><template slot=”title”></template
><el-menu-item index=”1-1″></el-menu-item><el-menu-item index=”1-2″></el-menu-item></el-menu-item-group><el-menu-item-group title=”分组2″><el-menu-item index=”1-3″></el-menu-item></el-menu-item-group><el-submenu index=”1-4″><template slot=”title”></template
><el-menu-item index=”1-4-1″></el-menu-item></el-submenu></el-submenu><el-submenu index=”2″><template slot=”title”><i class=”el-icon-menu”></i></template
><el-menu-item-group><template slot=”title”></template
><el-menu-item index=”2-1″></el-menu-item><el-menu-item index=”2-2″></el-menu-item></el-menu-item-group><el-menu-item-group title=”分组2″><el-menu-item index=”2-3″></el-menu-item></el-menu-item-group><el-submenu index=”2-4″><template slot=”title”></template
><el-menu-item index=”2-4-1″></el-menu-item></el-submenu></el-submenu><el-submenu index=”3″><template slot=”title”><i class=”el-icon-setting”></i></template
><el-menu-item-group><template slot=”title”></template
><el-menu-item index=”3-1″></el-menu-item><el-menu-item index=”3-2″></el-menu-item></el-menu-item-group><el-menu-item-group title=”分组2″><el-menu-item index=”3-3″></el-menu-item></el-menu-item-group><el-submenu index=”3-4″><template slot=”title”></template
><el-menu-item index=”3-4-1″></el-menu-item></el-submenu></el-submenu></el-menu></el-aside><el-container><el-header style=”text-align: right; font-size: 12px”><el-dropdown><i class=”el-icon-setting” style=”margin-right: 15px”></i><el-dropdown-menu slot=”dropdown”><el-dropdown-item></el-dropdown-item><el-dropdown-item></el-dropdown-item><el-dropdown-item></el-dropdown-item></el-dropdown-menu></el-dropdown><span></span></el-header><el-main><el-table :data=”list”><el-table-column prop=”datatext” label=”日期” width=”140″></el-table-column><el-table-column prop=”version” label=”姓名” width=”120″></el-table-column><el-table-column prop=”id” label=”地址”></el-table-column><el-table-column prop=”text” label=”描述”></el-table-column></el-table></el-main></el-container></el-container></template>template

css js:

<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>

<script>

export default {

  data () {
    const item = {
      date: '2016-05-02',
      name: '王小虎',
      address: '上海市普陀区金沙江路 1518 弄'

    }

    return {
      list: [],
      tableData: Array(20).fill(item)
    }
  },

  created () {
    this.getlist()
  },
  methods: {
    getlist () {
      let _this = this
      _this.$http.get('https://localhost:44314/api/Values').then(res => {
        _this.list = res.data
      })
    }
  }
}
</script>

View Code

<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>

<script>

export default {

  data () {
    const item = {
      date: '2016-05-02',
      name: '王小虎',
      address: '上海市普陀区金沙江路 1518 弄'

    }

    return {
      list: [],
      tableData: Array(20).fill(item)
    }
  },

  created () {
    this.getlist()
  },
  methods: {
    getlist () {
      let _this = this
      _this.$http.get('https://localhost:44314/api/Values').then(res => {
        _this.list = res.data
      })
    }
  }
}
</script>
<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>

<script>

export default {

  data () {
    const item = {
      date: '2016-05-02',
      name: '王小虎',
      address: '上海市普陀区金沙江路 1518 弄'

    }

    return {
      list: [],
      tableData: Array(20).fill(item)
    }
  },

  created () {
    this.getlist()
  },
  methods: {
    getlist () {
      let _this = this
      _this.$http.get('https://localhost:44314/api/Values').then(res => {
        _this.list = res.data
      })
    }
  }
}
</script>


.el
header {
background
color: #b3c0d1;
color: #
;
line
height: 60px;
}

.elaside {
color: #
;
}

export default {

data () {
const item {
date:
,
name:
,
address:

}

return {
list: [],
tableData: Array(
).fill(item)
}
},

created () {
this.getlist()
},
methods: {
getlist () {
let _this
this
_this.$http.get(
{
_this.list
res.data
})
}
}
}
View Code

数据填充页面

页面:
  <el-main>
        <el-table :data="list">
          <el-table-column prop="datatext" label="日期" width="140">
          </el-table-column>
          <el-table-column prop="version" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="id" label="地址"> </el-table-column>
          <el-table-column prop="text" label="描述"> </el-table-column>
        </el-table>
      </el-main>
页面:
  <el-main>
        <el-table :data="list">
          <el-table-column prop="datatext" label="日期" width="140">
          </el-table-column>
          <el-table-column prop="version" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="id" label="地址"> </el-table-column>
          <el-table-column prop="text" label="描述"> </el-table-column>
        </el-table>
      </el-main>

页面:
<el-main><el-table :data=”list”><el-table-column prop=”datatext” label=”日期” width=”140″></el-table-column><el-table-column prop=”version” label=”姓名” width=”120″></el-table-column><el-table-column prop=”id” label=”地址”></el-table-column><el-table-column prop=”text” label=”描述”></el-table-column></el-table></el-main>

 

运行如下

 

» 本文来自:前端开发者 » 《vue-Element-axios搭建调用api进行数据展示_情愫_前端开发者》
» 本文链接地址:https://www.rokub.com/73259.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!