vue.js - laravel使用vue-resource,报错Uncaught SyntaxError: Unexpected token
世界只因有你
世界只因有你 2017-05-16 16:48:59
[PHP讨论组]

按照vue-resource官方文档,以及laravel官方文档都显示应该采用如下语法格式:

var demo = new Vue({
  el: '#app',
  data: {
    gridColumns: {'#':'id', '公司名':'name', '组织名':'email', '电话':'created_at'},
    gridData: []
  },
  methods: {
    this.$http.get('../db').then((response) => {
      this.gridData = response.data;
    },(response) => {
      console.log(response);
    });
  }
});

但是浏览器直接报错:(index):51 Uncaught SyntaxError: Unexpected token .

经过多方资料查找,以及调试,最终发现可以正常运行的语法如下:

var demo = new Vue({
  el: '#app',
  data() {
    return{
      gridColumns: {'#':'id', '公司名':'name', '组织名':'email', '电话':'created_at'},
      gridData: []
    }
  },
  mounted(){
    this.$http.get('../db').then((response) => {
      this.gridData = response.data;
    },(response) => {
      console.log(response)
    });
  }
});

我想问的是,具体是什么原因导致的,以后的语法规则应该遵循哪一种?

补充:

世界只因有你
世界只因有你

全部回复(2)
黄舟

單純語法錯誤,你仔細看下第一個報錯的代碼

  methods: {
    // 這裡是對象呀,不能直接塞
    this.$http.get('../db').then((response) => {
      this.gridData = response.data;
    },(response) => {
      console.log(response);
    });
  }

應該是

  methods: {
    fetchData() {
        this.$http.get('../db').then((response) => {
          this.gridData = response.data;
        },(response) => {
          console.log(response);
        });
    }
  },
  mounted() {
      this.fetchData()
  }
天蓬老师

谢谢,Tomoe回复我的问题!
data的写法也知道为什么了。根据vue文档说明,在组件中不能用属性方式定义data,必须使用对象方式定义。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号