首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember应用程序无法从本地文件加载json数据

Ember应用程序无法从本地文件加载json数据
EN

Stack Overflow用户
提问于 2017-07-08 23:34:49
回答 2查看 1.1K关注 0票数 1

我一直在遵循ember快速入门指南来创建一个应用程序来显示一些数据(https://guides.emberjs.com/v2.13.0/getting-started/quick-start/),但我想显示以下json中的产品,而不是只显示一个带有科学家姓名的javascript数组。

我已经将json文件放在了公共文件夹中。

看起来是这样的:

代码语言:javascript
复制
{
  "products": [
    {
      "_id": "58ff60ffcd082f040072305a",
      "slug": "apple-tree-printed-linen-butterfly-bow-tie",
      "name": "Apple Tree Printed Linen Butterfly Bow Tie ",
      "description": "This fun 40 Colori Apple Tree Printed Linen Butterfly Bow Tie features a design of various apple trees built from tiny polka dots. The back of this bow tie features a polka dot print in complementing colours which when the bow tie is tied will pop out from behind making for a subtle yet unique detail. The playful design, stand out natural linen texture, and colourful combinations make this bow tie a perfect accessory for any outfit!\n",
      "standard_manufacturer": "58cbafc55491430300c422ff",
      "details": "Size: Untied (self-tie) bow tie with an easily adjustable neck strap from 13-3/4'' to 19'' (35 to 48 cm)\nHeight: 6 cm\nMaterial: Printed 100% linen\nCare: Dry clean\nMade in Italy",
      "sizes": [
        {
          "value": "Violet",
          "size": "57722c80c8595b0300a11e61",
          "_id": "58ff60ffcd082f0400723070",
          "marked_as_oos_at": null,
          "quantity": -1,
          "stock": true,
          "id": "58ff60ffcd082f0400723070"
        },

诸若此类。

我为显示列表的路由模型编写的代码如下

代码语言:javascript
复制
import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        //return ['Marie Curie', 'Mae Jemison', 'Albert Hofmann'];
        return Ember.$.getJSON("/products.json");
    }
});

我完全遵循了本教程,除了

代码语言:javascript
复制
return Ember.$.getJSON("/products.json");

scientists.js中的行。我的数据没有被显示,并且我在ember检查器中得到的错误是

代码语言:javascript
复制
compiled.js:2 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
    at E (compiled.js:2)
    at Object.u (compiled.js:25)
    at compiled.js:25
E   @   compiled.js:2
u   @   compiled.js:25
(anonymous) @   compiled.js:25

我对ember非常陌生,对js也很陌生。感谢任何人的帮助!谢谢!

EN

回答 2

Stack Overflow用户

发布于 2017-07-09 12:11:48

Ember附带了Development server(localhost:4200),它不能像with服务器那样使用,也不能用来响应ajax请求。您不能使用localhost:4200端点进行Ember.$.getJSON("/products.json")

你需要后端任何You服务器来返回响应。如果你目前还没有,那么出于开发目的,你需要使用动态渲染数据,那么我更喜欢使用ember-cli-mirage插件。

http://www.ember-cli-mirage.com/docs/v0.3.x/

票数 1
EN

Stack Overflow用户

发布于 2017-07-09 14:50:01

如果将JSON放在app文件夹中并将其导入呢?

代码语言:javascript
复制
import Ember from 'ember';
import yourData from 'your-app/json-file-name.js';

export default Ember.Route.extend({
  model() {
    return yourData;
  }
});

https://ember-twiddle.com/afb9d71933322860938b3936d617a776 -您可以使用它来尝试让.json正常工作...

这里也有一个线程:https://discuss.emberjs.com/t/how-to-import-data-from-json-file-to-ember-app

但是..。不要期望它能正确地出现在ember中--数据存储--并且像你在很多情况下所期望的那样工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44987848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档