码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【前端】Vue网络应用


    目录

    一、网络应用

    1.1axios

    1.2axios+vue


    一、网络应用

    Vue结合网络数据开发应用

    1.1axios

    功能强大的网络请求库

    1. axios.get(地址?key1=value1&key2=value2).then(function(response){},function(err){})
    2. axios.post(地址{key1=value1,key2=value2}).then(function(response){},function(err){})
    •  axios必须先导入才可以使用
    • 使用get或post方法即可发送对应的请求
    • then方法中的回调函数会在请求成功或者失败时触发
    • 通过回调函数的形参可以获取响应内容或错误信息
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <button class="get">get请求button>
    11. <button class="post">post请求button>
    12. <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    13. <script>
    14. /* 随机笑话
    15. 请求地址:https//autumnfish.cn/api/joke/list
    16. 请求方法:get
    17. 请求参数:num
    18. 响应内容:随机笑话 */
    19. document.querySelector(".get").onclick = function () {
    20. axios.get("https://autumnfish.cn/api/joke/list?num=6").then(function (response) {
    21. console.log(response);
    22. }, function (err) {
    23. console.log(err);
    24. })
    25. }
    26. /* 用户注册
    27. 请求地址:https://autumnfish.cn/api/user/reg
    28. 请求方法:post
    29. 请求参数:username
    30. 响应内容:注册成功或失败 */
    31. document.querySelector(".post").onclick = function () {
    32. axios.post("https://autumnfish.cn/api/user/reg", { username: "jack" }).then(function (response) {
    33. console.log(response);
    34. }, function (err) {
    35. console.log(err);
    36. })
    37. }
    38. script>
    39. body>
    40. html>

    文档传送门:GitHub - axios/axios: Promise based HTTP client for the browser and node.js

    1.2axios+vue

    1. <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    2. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
    • axios回调函数中的this已经改变,无法访问到data中数据
    • 把this保存起来,回调函数中直接使用保存的this即可
    • 和本地应用的最大区别就是改变了数据来源
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <div id="app">
    11. <button @click="getJoke">获取笑话button>
    12. <p>{{joke}}p>
    13. div>
    14. <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    15. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
    16. <script>
    17. /* 接口:随机获取一条笑话
    18. 请求地址:https://autumnfish.cn/api/joke
    19. 请求方法:get
    20. 请求参数:无
    21. 响应内容:随机笑话 */
    22. var app = new Vue({
    23. el: "#app",
    24. data: {
    25. joke: "很好笑的笑话"
    26. },
    27. methods: {
    28. getJoke: function () {
    29. var that = this;
    30. axios.get("https://autumnfish.cn/api/joke").then(function (response) {
    31. that.joke = response.data;
    32. },
    33. function (err) {
    34. console.log(err);
    35. })
    36. }
    37. }
    38. })
    39. script>
    40. body>
    41. html>
  • 相关阅读:
    安装 Gin 框架
    华为云项目部署
    领悟《信号与系统》之 信号与系统的描述-下节
    C++11之decltype类型推导(使用场景、推导四规则、cv限定符)
    webpack优化篇(四十二): 使用高版本的 webpack 和 Node.js
    LeetCode高频题300. 最长递增子序列
    苹果服务器退款通知处理notificationType为CONSUMPTION_REQUEST
    react 路由的组件说明、路由的执行过程
    8.【外部排序】基本概念和方法 + 优化:【败者树】{减少关键字对比次数}、【置换-选择 排序】{减少初始归并段数量}、【最佳归并树】{谁先合并更快}
    计算机网络-防火墙简单配置实验
  • 原文地址:https://blog.csdn.net/qq_45764950/article/details/127858847
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号