Laravel5.4 vuejs和axios使用钩子mounted不能获取属性data的解决方法

//出错问题:
then 这个里边的赋值方法this.followed = response.data.followed会出现报错,什么原因呢?原来是在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定。解决:self.followed = response.data.followed;或者使用 ES6 的 箭头函数arrow function,箭头方法可以和父方法共享变量。

Here is my code:
数据属性:

 data(){
        return {
            followed : false,
        }
    },

axios请求数据:

 // mounted 方法为钩子,在Vue实例化后自动调用
    mounted() {
       axios.post('/api/question/follower', {
           'question':this.question,
           'user':this.user
       }).then(function(response){
           // console.log(response.data);
           this.followed = response.data.followed;
       })
    },

出错问题:
then 这个里边的赋值方法this.followed = response.data.followed会出现报错,什么原因呢?在Google上查了下,原来是在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定。
可以看下 Stackoverflow 的解释:

In option functions like data and created, vue binds this to the view-model instance for us, so we can use this.followed, but in the function inside thenthis is not bound.

So you need to preserve the view-model like (created means the component's data structure is assembled, which is enough here, mounted will delay the operation more):

解决方法:

    created() {
       var self = this;
       axios.post('/api/question/follower', {
           'question':this.question,
           'user':this.user
       }).then(function(response){
           // console.log(response.data);
           self.followed = response.data.followed;
       })
    },

或者我们可以使用 ES6 的 箭头函数arrow function,箭头方法可以和父方法共享变量 :

 created() {
       axios.post('/api/question/follower', {
           'question':this.question,
           'user':this.user
       }).then((response) => {
           this.followed = response.data.followed;
       })
    },

完整代码:

 
<script>
export default {
    // 为父组件传递到子组件的属性值,子组件使用props方法接收
    props:['question', 'user'],
    // mounted 方法为钩子,在Vue实例化后自动调用
    mounted() {
       /**  这种旧的写法会在Laravel5.4中报错
        this.$http.post('/api/question/follower', {'question':this.question, 'user':this.user}).then(response => {
            console.log(response.data);
        })
        */
       axios.post('/api/question/follower', {
           'question':this.question,
           'user':this.user
       }).then(function(response){
           // console.log(response.data);
           this.followed = response.data.followed;
       })
    },
    data(){
        return {
            followed : false,
        }
    },
    computed:{
        text(){
           return this.followed ? '已关注' : '关注该问题';
        }
    },
    methods:{
        // 关注动作
        follow(){
            axios.post('/api/question/follow', {
                'question':this.question,
                'user':this.user
            }).then(function(response){
                this.followed = response.data.followed;
            })
        }
    }
}
</script>

在vuejs 中使用axios不能获取属性data的解决方法的更多相关文章

  1. jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法

    jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法 工作中用到一个具有多选功能的easyui-datagrid在处理cell的点击事件时,不同 ...

  2. [datatable]关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法

    -- :09关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在实际编程工程中,常常遇到这样的情况:DataTable并不 ...

  3. 检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下:

    检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下: 第 一步:首先将msvcr71.dll,  SQLD ...

  4. log4j中Spring控制台输出Debug级信息过多解决方法

    log4j中Spring控制台输出Debug级信息过多解决方法 >>>>>>>>>>>>>>>>> ...

  5. universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法

    在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...

  6. MySQL中遇到的几种报错及其解决方法

    MySQL中遇到的几种报错及其解决方法 1.[Err] 1064 - You have an error in your SQL syntax; check the manual that corre ...

  7. Eclipse中SVN修改的*星号没了,解决方法

    Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...

  8. BootStrap iCheck插件全选与获取value值的解决方法

    这篇文章主要介绍了BootStrap iCheck插件全选与获取value值的解决方法,解决方法其实很简单,下面小编给大家分享下这方面的知识 在使用jQuery iCheck 插件的时候遇到了一个问题 ...

  9. Python3中使用HTMLTestRunner报No module named 'StringIO'解决方法

    今天在学习使用HTMLTestRunner生成测试报告时遇到一个报错,如图所示: 网上搜索了下“No module named 'StringIO'”解决方法,原来我用的是Python 3.X版本,而 ...

随机推荐

  1. c语言学习笔记(3)——输入输出

    一.基本的输入和输出函数的用法 printf()  //屏幕输出 用法: (1)printf("字符串\n"); (2)printf("输出控制符", 输出参数 ...

  2. 自由WiFi软体,你也太简单了

    自由WiFi市场热点,自然不用多说.支付宝钱包计划实现全民免费WiFi,360.百度.金山.小米都在着手WiFi产品. 只是.这些免费WiFi或者实现WiFi的硬件或软件.都是争夺新的市场入口,推广产 ...

  3. 【转】mac版微信web开发者工具(小程序开发工具)无法显示二维码 解决方案

    转自:https://www.cnblogs.com/stevenluo/p/6030445.html   微信小程序概念的提出,绝对可以算得上中国IT界惊天动地的一件大事,这可能意味着一场新的开发热 ...

  4. Google CFO 辞职信

    Google CFO 辞职信   After nearly 7 years as CFO, I will be retiring from Google to spend more time with ...

  5. sql server 查询存储过程指令

    Sp_helptext PROCEDUREName 例子 创建存储过程 CREATE PROCEDURE SelectFromBitTable AS BEGIN select pkid,isdelet ...

  6. KEIL MDK编译后的代码量和RAM使用详解

    一般 MCU 包含的存储空间有:片内 Flash 与片内 RAM,RAM 相当于内存,Flash 相当于硬盘.编译器会将一个程序分为好几个部分,分别存储在 MCU 不同的存储区.Keil 工程在编译完 ...

  7. Go 的文件系统抽象 Afero

    Afero 是一个文件系统框架,提供一个简单.统一和通用的 API 和任何文件系统进行交互,作为抽象层还提供了界面.类型和方法.Afero 的界面十分简洁,设计简单,舍弃了不必要的构造函数和初始化方法 ...

  8. SQLite Expert Professional 打开加密SQLite数据库

    原文 SQLite Expert Professional 打开加密数据库 (已修改) 版本:sqlite expert professional 4.2.0.739 (x86) 目的:用SQLite ...

  9. 毕设(二)C#SerialPort

    毕业设计中,用到串口与无人机通信,所以就用到了SerialPort这个类,这个类在设置属性时, 用到最主要的属性应该是COM口和波特率,由于本人不熟悉硬件,不便多说,但经验告诉我是这样的, 还有数据位 ...

  10. QT5.1编译后的安装目录问题(硬路径问题)

    这个是我的编译参数:configure -ltcg -confirm-license -opensource -platform win32-msvc2010 -debug-and-release - ...