关于 vuejs2:\\”TypeError: Cannot read property \\’get\\’ of undefined\\”, Axios, Vue.JS | 珊瑚贝

“TypeError: Cannot read property ‘get’ of undefined”, Axios, Vue.JS


尝试使用 axios 从 api 访问获取请求时,我从 Vue 收到这个奇怪的错误,
我收到 “TypeError: Cannot read property \\’get\\’ of undefined”

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 <template>
   
        {{ msg }}
        <p>Welcome to your new single-page application, built with Vue.js.</p>
   
</template>

    var Vue = require(‘vue’);

   // import axios from”axios”;

    window.axios=require(‘axios’);
    export default {
        name: ‘Home’,
        props: {
            msg: String
        },
        component: {
        },  
        mounted: function () {
            alert(‘Hi ‘);
            this.axios.get(‘https://api.github.com/users’)
                .then(value => {
                    alert(value);
                         
                });
        }

    };

<!– Add”scoped” attribute to limit CSS to this component only –>
<style scoped>
</style>

  • 它应该只是 axios.get,而不是 this.axios.get。您可能会看到在某些示例中使用了 this.axios,但在这些情况下,它们已将 axios 存储在 this 上,而在您的代码中似乎并非如此。
  • 我还建议坚持使用 import 方法,而不是使用 require 和 window。
  • 谢谢@skirtle,非常感谢先生:)


this 在您的情况下不引用 window 。更好的方法是在组件中导入 axios:

1
import axios from ‘axios’;

更有效的方法是在 main.js 文件中全局设置一次:

1
Vue.prototype.$http = axios

并在任何你想要的地方使用它 this.$http


来源:https://www.codenong.com/63651860/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8919.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?