码迷,mamicode.com
首页 > 移动开发 > 详细

Axios和Mockjs,玩起~~~

时间:2020-01-17 00:13:28      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:button   mamicode   pre   from   log   技术   rip   temp   rem   

 

 

router/index.js

import Vue from ‘vue‘
import VueRouter from ‘vue-router‘
import Home from ‘@/views/Home.vue‘
import Login from ‘@/views/Login.vue‘
import About from ‘@/views/About.vue‘
import NotFound from ‘@/views/404.vue‘

Vue.use(VueRouter)

const routes = [
  {
    path: ‘/‘,
    name: ‘home‘,
    component: Home
  },
  {
    path: ‘/about‘,
    name: ‘about‘,
    component: About
  },
  {
    path: ‘/login‘,
    name: ‘login‘,
    component: Login
  },
  {
    path: ‘/404‘,
    name: ‘notFound‘,
    component: NotFound
  }

]

const router = new VueRouter({
  mode: ‘history‘,
  base: process.env.BASE_URL,
  routes
})

export default router

views/about.vue

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <el-button type="primary" @click="testAxios()">Test Axios</el-button>
    <el-button type="primary" @click="getUser()">UserInfo</el-button>
    <el-button type="primary" @click="getMenu()">Menu</el-button>
  </div>
</template>

<script>
import axios from ‘axios‘
import mock from ‘@/mock/mock.js‘
export default {
    name: "about",
    methods: {
        testAxios() {
            axios.get(‘http://127.0.0.1:8080/‘).then(res => { alert(res.data) })
        },
        getUser() {
            axios.get(‘http://127.0.0.1:8080/user‘).then(res => { alert(JSON.stringify(res.data)) })
        },
        getMenu() {
            axios.get(‘http://127.0.0.1:8080/menu‘).then(res => { alert(JSON.stringify(res.data)) })
        },
    }
}
</script>

mock/mock.js

import Mock from ‘mockjs‘

Mock.mock(‘http://127.0.0.1:8080/user‘, {
    ‘name‘: ‘@name‘,
    ‘email‘: ‘@email‘,
    ‘age|1-10‘: 5,
})

Mock.mock(‘http://127.0.0.1:8080/menu‘, {
    ‘id‘: ‘@increment‘,
    ‘name‘: ‘menu@increment‘,
    ‘order|1-20‘: 5,
})

技术图片

Axios和Mockjs,玩起~~~

标签:button   mamicode   pre   from   log   技术   rip   temp   rem   

原文地址:https://www.cnblogs.com/aguncn/p/12203549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!