首页 > 解决方案 > 从文件和 Ember CLI Mirage 中获取内容

问题描述

我的 Ember 应用程序中有一个组件,它从静态降价文件返回数据。

  didInsertElement() {
    fetch('markdown/faqs-content.md').then(response => {
      response.text().then(result => {
        this.set('markdown', result);
      });
    });
  }

当应用程序运行时,请求转到http://localhost:4200/markdown/faqs-content.md并且一切正常。

但是,在海市蜃楼中,我收到此错误:

Mirage: Your app tried to GET '/markdown/faqs-content.md', but there was no route defined to handle this request. Define a route for this endpoint in your routes() config. Did you forget to define a namespace?

即使以下内容包含在以下内容中,也会发生上述情况mirage/config.js

this.passthrough();

据推测,这是因为请求 URL 中不包含 API 命名空间。

可以通过将完整的 URL 添加到 passthrough 来解决该问题:

this.passthrough('https://localhost:4201/markdown/faqs-content.md');

这样做有两个问题:

  1. 我并不总是知道应用程序所在的端口,并且
  2. 我不想失去 using 的功能this.passThrough(),它会自动为任何 API 请求调用 pass through,如果mirage/config.js.

因此,我有 2 个问题。

  1. config.js中,有没有办法获取 Ember 服务器正在运行的端口,以允许类似的东西https://localhost:${port}/markdown/faqs-content.md

  2. 有没有一种方法可以将 Mirage 配置为传递给 的请求https://localhost:4201/markdown/faqs-content.md,并且仍然允许它传递给没有定义相应路由的任何其他请求?

标签: ember.jsember-cli-mirage

解决方案


直通(以及一般的路由 API)有一些不幸的奇怪行为,基于它的编码方式 + 它用来拦截请求的底层库。我们需要更新指南,因为其他人已经遇到了这个问题。

如果你把

this.namespace = 'api'
this.passthrough()

那么 Mirage 将让所有请求通过/api/*,但如果您将调用移至passthrough之前,或者将命名空间“重置”为空字符串,它应该处理对当前域的所有请求。

我需要测试,但我会尝试两者

this.namespace = ''
this.passthrough()

this.passthrough('/**')

看看他们是否工作。(我们真的需要向 Mirage 添加一个嵌套路由 DSL!它会删除所有这些笨拙的 API。)


推荐阅读