美文网首页
vue+gin前后端分离

vue+gin前后端分离

作者: heliping_peter | 来源:发表于2020-02-06 14:41 被阅读0次

1.gin

很简单,写个api接口

package main
 
import (
    "fmt"
    "log"
    "net/http"
)

func index(w http.ResponseWriter, r *http.Request) { 
    fmt.Fprintf(w, "Hello golang http!")
}
 
func main() {
    http.HandleFunc("/home", index)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

2.vue

搭建vue环境,两个单页面


image.png

main.js
需要使用路由和axios

import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
import axios from 'axios'
 
Vue.config.productionTip = false
Vue.prototype.$axios = axios
new Vue({
 
  router,
  render: h => h(App),
}).$mount('#app')

vue.config.js
需要配置跨域的代理

module.exports = {
    publicPath: '/',
    outputDir: 'dist',
    devServer: {
      open: true,
      host: 'localhost',
      port: '8081',
      proxy: {
        '/api': {
          target: 'http://localhost:9090', // 要请求的地址
          ws: true,
          changeOrigin: true,
          pathRewrite: {
            '^/api': ''
          }
        }
      }
    }
  };

router.js
两个页面的路由

import Vue from 'vue'
import Router from 'vue-router'
import Home from './components/Home.vue'
import Abc from './components/Abc.vue'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home
    },
    {
      path: '/abc',
      name: 'abc',
      component: Abc
    }
  ]
})

app.js
使用路由

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>


export default {
  name: 'app'

}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

abc.vue
需要调用后台的api

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>
      gogogo,{{ msg }}
     
    </p>

   
  </div>
</template>

<script>

export default {
  name: 'Abc',
  data() {
    return {
      msg: ''
    }
    
  },
  created() {
        this.$axios.get('/api/home').then(result=>{            
            this.msg = result.data;
        })
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

相关文章

  • vue+gin前后端分离

    1.gin 很简单,写个api接口 2.vue 搭建vue环境,两个单页面 main.js需要使用路由和axios...

  • 2019-01-11前后端分离

    什么是前后端分离? 为什么前后端分离? 前后端分离的优势? 未分离时期 MVC: 早期JSP+SERVLET中的结...

  • 前后端分离

    什么是前后端分离 前后端分离中前端负责页面路由控制,页面展示,后端处理数据,通过json进行传输。前后端分离并非仅...

  • vivo 商城前端架构升级—前后端分离篇

    本文主要以 vivo 商城项目的前后端分离经验,总结前后端分离思路,整理前后端分离方案,以及分离过程中遇到的问题及...

  • Spring Boot+Vue概述(一)

    前后端分离 前后端分离就是将⼀个应⽤的前端代码和后端代码分开写,为什么要这样做?如果不使⽤前后端分离的⽅式,会有哪...

  • 前后端分离

    方案一 简易前后端分离 前后端分离原则,简单来讲就是前端和后端的代码分离,也就是技术上做分离,我们推荐的模式是最好...

  • 六大接口管理平台,总有一款适合你的!

    前后端分离绕不开的接口测试 先聊一聊前端和后端分离的优点。前后端分离优点如下: 真正的实现前后端解耦,前端服务器使...

  • 使用nginx解决跨域问题

    1.跨域解释 1.1 怎么知道我遇到了跨域问题 如果项目没做前后端分离,是不会有跨域问题的。前后端分离的项目中,前...

  • 前后端分离架构与小程序的环境切换

    前后端分离架构 随着前端应用的越来越复杂,前后端分离架构成为了主流。前后端分离之后,前端并不依赖后端的模板和路由,...

  • 基于Flask开发的前后端分离租房项目(一)

    一、明确前后端分离和前后端不分离的概念: 我的理解:前后端不分离的概念是后端要控制前端的数据显示和模板渲染(dja...

网友评论

      本文标题:vue+gin前后端分离

      本文链接:https://www.haomeiwen.com/subject/zkxuxhtx.html