【问题标题】:reverse array issue in VueJsVueJs中的反向数组问题
【发布时间】:2017-01-24 20:01:44
【问题描述】:

我通过 facebook graph api 获得对话消息,我需要反转数据。

在计算的 reverseChat 函数中,当我尝试 JSON.parse(this.chat).data.reverse()this.chat.data.reverse() 错误显示时,我尝试返回 this.chat.data 有效,但 reverse() 无效。

控制台显示错误

[Vue warn]: Error when rendering anonymous component at C:\Users\PEM\Desktop\src\components\Chat.vue:
SyntaxError: Unexpected end of JSON input
    at json.parse (<anonymous>)
    at Proxy.render (eval at <anonymous> (app.js:2430), <anonymous>:123:85)
    at VueComponent.Vue._render (ever at <anonymous> (app.js:606), <anonymous>:2464:21)
    ...

jsfiddle 中的示例代码

https://jsfiddle.net/pqrf2vu4/

【问题讨论】:

  • 反向是什么意思?请花点时间更好地解释您的需求和问题
  • 您似乎正在尝试反转对象。 reverse() 需要在数组上使用。你为什么要反转chat?您是否可能尝试在聊天中反转 data 数组?
  • 您的聊天属性中有一个 json 字符串。解析它然后您可以访问数据属性。 JSON.parse(this.chat).data.reverse()。请看here。 (请更新您的问题,以便问题明确,我会添加答案。)
  • 我在我的问题中添加了图片错误,我想要像你的链接一样的结果反向我在 .vue 中尝试 JSON.parse (this.chat) .data.reverse () 但错误。
  • 嗨佩米!您可以编辑问题,删除链接的图像并将错误消息粘贴为文本吗?

标签: vuejs2 vue.js


【解决方案1】:
JSON.parse(this.chat).data.reverse()

有效。见Jsfiddle

【讨论】:

  • 在 jsfiddle 中工作,但在 file.vue 中我的项目错误我不知道为什么
  • 可能在计算时this.chat没有数据?给this.chat一个默认值{data:{}}看看是否有效。
  • 其实这个 - chat: '{"data": [] }'
  • 在计算的 reverseChat 函数中,当我尝试 JSON.parse(this.chat).data.reverse()this.chat.data.reverse() 错误显示时,我尝试返回 this.chat.data 有效,但 reverse() 无效。
  • 尝试在return this.chat &amp;&amp; this.chat.data ? this.chat.data.reverse() : '' 中做reversedChat()
猜你喜欢
  • 2019-11-09
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2021-01-16
相关资源
最近更新 更多