刷新 ember 页面时如何避免 "Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number"?

How can I avoid "Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number" when refreshing an ember page?

Ember 有一个非常烦人的功能,我不知道如何解决。我可能有一个 url 看起来像下面的

http://{my-blog-name}/posts/view/{some-blogpost-ID}

我进入此页面的方法是单击 {my-blog-name}/posts 页面内的 link。这有效并将按预期显示页面。但是,如果我尝试刷新页面,或者如果我只是在我的 url 搜索框中输入 http://{my-blog-name}/posts/view/{some-blogpost-ID},我将得到

Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number

这是我导航到 posts/view/{some-blog-id} 页面的方式。

post.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('post');
    }
});

posts.hbs

<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li> 

view.js

import Ember from 'ember';

var siteId;

export default Ember.Route.extend({
    model(params) {
        siteId = params.site_id;
        return this.store.findRecord('post', params.site_id);
    }
});

view.hbs

<div id="Links">
  <h1 id="blog-header-title">My Blog</h1>
  <!--<p>{{!#link-to 'welcome'}} See about me{{!/link-to}}</p>-->
  {{outlet}}
</div>

{{outlet}}

router.js

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

const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('index', { path: '/' }); // This is usually automatic if path undeclared, but declared here to support /index below
  this.route('posts', function() {
    this.route('view', {path: '/view/:post_id'});
  });
  this.route('welcome');
}

这真令人沮丧,因为这意味着我无法创建博客 post 并与朋友分享 link。为什么会发生这种情况,是否有解决它的好方法?

posts.js 路由返回所有可用的 post,即 RecordArray

<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li>

so 在上面 posts - 指的是单个 post 模型或 post 模型的 RecordArray ?。如果以上是单一模型,那么您将在 view.js 的模型挂钩中收到 params.post_id,目前您正在使用 params.site_id 而不是 params.post_id


未执行模型挂钩的原因。
https://guides.emberjs.com/v2.13.0/routing/specifying-a-routes-model/#toc_dynamic-models

Note: A route with a dynamic segment will always have its model hook called when it is entered via the URL. If the route is entered through a transition (e.g. when using the link-to Handlebars helper), and a model context is provided (second argument to link-to), then the hook is not executed. If an identifier (such as an id or slug) is provided instead then the model hook will be executed.