【问题标题】:Ember manual url change does not load pageEmber 手动 url 更改不加载页面
【发布时间】:2017-01-08 13:56:08
【问题描述】:

我有一个使用 Coffeescript 和 Ember 2.7.1 的 EmberJS 应用程序。

我将/ 重定向到/student

当我打开我的应用程序时,假设在www.something.com/~somebody/dist/(是的,我需要~somebody/dist 部分),它按预期转到www.something.com/~somebody/dist/student

我还有其他页面,例如/settings。如果我使用 link-to 助手进入设置页面,它就可以工作。当我手动将 url 从www.something.com/~somebody/dist/student 更改为www.something.com/~somebody/dist/settings 时,它不会加载页面。

我收到加载资源失败:服务器响应状态为 404(未找到) 错误。

知道如何解决这个问题吗?

我的 router.coffee 文件:

`import Ember from 'ember'`
`import config from './config/environment'`

Router = Ember.Router.extend
    location: config.locationType,
    rootURL: config.rootURL

Router.map ->
    @route 'student'
    @route 'settings'
    @route 'statistics'
    @route 'directory'

`export default Router`

我的 routes/index.coffee 文件:

`import Ember from 'ember'`

IndexRoute = Ember.Route.extend
  beforeModel: ->
      @transitionTo('student')

 `export default IndexRoute`

我的 routes/settings.coffee 文件:

`import Ember from 'ember'`

SettingsRoute = Ember.Route.extend()

`export default SettingsRoute`

我的 routes/student.coffee 文件:

`import Ember from 'ember'`

StudentRoute = Ember.Route.extend()

`export default StudentRoute`

我的 environment.js 文件:

    /* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'something-frontend',
    environment: environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    }
  };

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.baseURL = '/';
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {
      ENV.location = 'hash';
      ENV.rootURL = '/~somebody/dist'
  }

  return ENV;
};

【问题讨论】:

    标签: ember.js coffeescript url-routing


    【解决方案1】:

    您需要有一个设置路线才能让您的应用程序进入。请确保您有 routes/settings.js 文件。

    【讨论】:

    • 我有那个文件。我的所有路线都有这些文件。我编辑了我的问题以使其更清楚。
    【解决方案2】:

    这不是 ember 的问题,而是 http 服务器配置的问题。您应该配置您的 http 服务器以将所有与文件不匹配的请求重定向到 index.html。

    在 nginx 中你可以使用以下内容:

    location /~user/dist/ {
      try_files $uri $uri/ /index.html?/$request_uri;
    }
    

    【讨论】:

    • 谢谢,但最后我不必尝试这个,因为我在 Ember 中找到了一个“解决方法”。 :)
    【解决方案3】:

    最后我解决了在 environment.js 中将 locationType 设置为 hash 的问题。 locationType: 'hash'

    http://emberjs.com/api/classes/Ember.Location.html#toc_hashlocation

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2013-01-02
      • 2016-10-28
      相关资源
      最近更新 更多