当前位置: 首页 > news >正文

国际交流中心网站建设与管理制度做财经比较好的网站有哪些

国际交流中心网站建设与管理制度,做财经比较好的网站有哪些,大型门户网站核心技术,做网站和推广的公司哪家好vue3koaaxios实现前后端通信 写了一个小demo来实现前后端通信,涉及跨域问题,非常简单可以给大家平时开发的时候参考 服务端: 目录结构如下: router index.js // router的入口文件 // 引入路由 const Router require("koa-router&quo…

vue3+koa+axios实现前后端通信

写了一个小demo来实现前后端通信,涉及跨域问题,非常简单可以给大家平时开发的时候参考

服务端:
目录结构如下:
在这里插入图片描述
router index.js


// router的入口文件
// 引入路由
const Router = require("koa-router")
const router = new Router()
router.get("/", async (ctx) => {ctx.body = "首页"})router.get("/list",async(ctx)=>{ctx.body={data:[{name:1},{name:2}]}})
// router.use()
router.use(router.routes(), router.allowedMethods())// 一般首页是直接访问ip+端口号进入,所以可以将期重定向到/home下的某一个路由
router.redirect("/", "/list")module.exports = router // 导出router给app.js使用

app.js

// 整个koa项目的入口文件
const Koa = require("koa") // 引入koa
const app = new Koa() // 声明一个实例
const port = 3000 // 端口号
const router = require("./router/index") // 配置路由
const cors = require("koa-cors") // 解决跨域
const static = require("koa-static") // 静态资源管理
const path = require("path")/*** use()就是调用router中间件* router.routes()作用是启动路由* router.allowedMethods()作用是允许任何请求(例如:get,post,put)*/
//  router.get("/list",async(ctx)=>{
//     console.log(ctx)
//     ctx.body={
//         data:[{name:1},{name:2}]
//     }// })
app.use(static(path.join(__dirname + "/public"))) //读取静态资源
app.use(cors({exposeHeaders: ['WWW-Authenticate', 'Server-Authorization', 'x-show-msg'],maxAge: 5,  //  该字段可选,用来指定本次预检请求的有效期,单位为秒allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // 允许的请求方法allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Requested-With'] 
})) //后端允许跨域访问app.use(router.routes(), router.allowedMethods())app.listen(port, () => {console.log(`server in running at http://localhost:${port}`)
})

前端:
首先要安装axios
main.js

import { createApp } from 'vue'
import App from './App.vue'
import axios from 'axios'const app = createApp(App)
app.config.globalProperties.$axios = axios
app.mount('#app')

vue.config.js

module.exports = {devServer: {port:8080,open:true,proxy: {'/api': {target: 'http://localhost:3000/', //接口域名changeOrigin: true,             //是否跨域ws: true,                       //是否代理 websocketssecure: false,                   //是否https接口pathRewrite: {                  //路径重置'^/api': ''}}}}
};

前端请求数据:

<template><div class="hello"><button @click="sendMessage">click me</button>  <input type="text" :value="msg"></div>
</template><script src="./hello"></script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
import axios from "axios"
import {ref} from 'vue'export default {setup(){let msg=ref();function sendMessage(){axios.get('/api/list').then(function(res){console.log(res.data.data)msg.value=res.data.data[0].name})}return{msg,sendMessage}}
http://www.yayakq.cn/news/822003/

相关文章:

  • 中文域名网站 被搜索源码之家关闭了
  • 济宁三合一网站建设商品网站开发
  • 网站开发中视屏怎样编辑到网页上昆明优化官网服务
  • .net网站开发项目成都网站建设潮州
  • 做外贸可以在哪些网站注册网站建设培训班多少钱
  • 别人做的网站怎么安全放在我的公司网店推广网站
  • 门户网站建设投资门诊部网站建设
  • 网站编辑器失效长沙网站设
  • 南通 网络 公司网站备案网站建设
  • 做淘客网站注意事项一个简单的网站怎么做的
  • 二级网站内容建设要求足球比赛直播比分直播
  • 中小型企业网站建设一条龙建站
  • 公司网站公司网站网站托管维护合同
  • 青岛城阳网站开发网站建设图书推荐
  • 重庆门户网站推广方案wordpress single.php制作
  • 网站建站的流程wordpress 登录 查看
  • 安微省建设厅田网站注释网站开发
  • 网站建设的竞争力用c 怎么做网站系统
  • 没注册可以做网站吗redis 缓存 wordpress
  • 网站美工效果图怎么做企业网站建设分为那几个阶段
  • 广西智能网站建设哪家好网站做竞价经常会被攻击吗
  • 做一手楼房的网站wordpress加音乐
  • 有哪些网站可以做代理做网站推广价格
  • 个人网站数据库大小网站那个做的比较好的
  • 辽阳太子河网站建设装饰工程有哪些
  • 房屋中介做网站的书籍河源市seo点击排名软件价格
  • 加猛挣钱免费做网站软件济南seo网站推广
  • 一加网站开发视频网站app怎么做的
  • 哪些网站做免费送东西的广告商务网站策划书
  • 樟木头镇网站建设公司网站赚流量