【问题标题】:Fetching content from file and Ember CLI Mirage从文件和 Ember CLI Mirage 中获取内容
【发布时间】:2020-01-28 08:13:04
【问题描述】:

我的 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来解决问题:

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

这样做有两个问题:

  1. 我并不总是知道应用程序所在的端口,并且
  2. 我不想失去使用this.passThrough() 的功能,它会自动为mirage/config.js 中没有相应路由的任何API 请求调用传递。

因此我有 2 个问题。

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

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

【问题讨论】:

    标签: ember.js ember-cli-mirage


    【解决方案1】:

    Passthrough(以及一般的路由 API)有一些不幸的奇怪行为,基于它的编码方式 + 它用于拦截请求的底层库。我们需要更新指南,因为其他人也遇到过这种情况。

    如果你放

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

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

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

    this.namespace = ''
    this.passthrough()
    

    this.passthrough('/**')
    

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

    【讨论】:

    • 谢谢。我已经尝试了上面提到的所有方法,但不幸的是我仍然遇到同样的错误。
    • 复制 miragejs 问题将是我诊断的最简单方法!
    • 嗨,Sam,您找到解决方案了吗?
    【解决方案2】:

    就第二个问题而言,似乎以下解决了它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 2016-08-16
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多