zoukankan      html  css  js  c++  java
  • 【VUE】【axios】2种方法:数据库数据→传递→给vue中数据this.joke = response.data

    使用箭头函数可以访问上层的this

    否则需要使用新定义的that进行暂存

    结果:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <!-- 官方axios在线地址 -->
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <!-- 随机获取一条笑话的接口
        请求地址:https://autumnfish.cn/api/joke
        请求方法:get
        请求参数:无
        响应内容:随机笑话 -->
    
    </head>
    
    <body>
        <div id="app">
            <button value="获取笑话" @click="getJoke">获取笑话</button>
            <!-- 原来js用class添加的事件,vue中直接用@click解决了 -->
            <button value="获取笑话" @click="getJoke1">获取笑话1</button>
    
        </div>
        <script>
            var area = new Vue({
                el: "#app",
                data: {
                    joke: "这是一个笑话"
                },
                methods: {
                    getJoke: function() {
                        var that = this; //this会变,就存一下
                        axios.get("https://autumnfish.cn/api/joke?num=1").then(function(response) {
                            that.joke = response.data;                     }, function(error) {
                            console.log(error);
                        })
                    },
                    // 使用箭头函数可以访问上层的this,不用新增变量that进行暂存了
                    getJoke1: function() {
                        axios.get("https://autumnfish.cn/api/joke?num=1").then((response) => {
                            this.joke = response.data; //哦!!把数据库response里面的数据,传送给vue~~
                            console.log(this.joke); //成功~,不再是默认值"这是一个笑话"了
                        }, function(error) {
                            console.log(error);
                        })
    
                    }
                }
            })
        </script>
    </body>
    
    </html>
    
  • 相关阅读:
    Raphael入门
    EXT使用SASS修改主题样式问题
    Hough Transform Performance article List
    about utf8 ...
    computer vision resource
    Asp.net 中配置 CKEditor和CKFinder
    压缩格式 zip与tar.gz 区别
    一系列按钮的弹出提示框借助jq实现
    SQL Server2005 sa登录出错~
    正则表达式整理
  • 原文地址:https://www.cnblogs.com/icemiaomiao3/p/14621471.html
Copyright © 2011-2022 走看看