jor*_*dan 1 ember.js ember-data ember-cli-mirage
嘿,我正在浏览 emberjs 教程,但我遇到了这个无法解决的问题。当我尝试从 store 调用 findAll 函数时,它会抛出类型错误并表示 findAll 不是函数。当我使用 this.get() 方法时,它说它是一个经典的 ember 对象方法,并且不能在辛烷类中使用。有人知道如何解决这个问题吗?
在此先感谢您的时间!
应用程序/路线/rental.js
import Route from '@ember/routing/route';
export default class RentalsRoute extends Route {
model() {
return this.store.findAll('rental');
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/rental.js
import Model, { attr } from '@ember-data/model';
export default class RentalModel extends Model {
@attr title;
@attr owner;
@attr city;
@attr propertyType;
@attr image;
@attr bedrooms;
@attr description;
}
Run Code Online (Sandbox Code Playgroud)
海市蜃楼/config.js
export default function () {
this.namespace = '/api';
this.get('/rentals', function () {
return {
data: [
{
type: 'rentals',
id: 'grand-old-mansion',
attributes: {
title: 'Grand Old Mansion',
owner: 'Veruca Salt',
city: 'San Francisco',
'property-type': 'Estate',
bedrooms: 15,
image:
'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg',
},
},
{
type: 'rentals',
id: 'urban-living',
attributes: {
title: 'Urban Living',
owner: 'Mike Teavee',
city: 'Seattle',
'property-type': 'Condo',
bedrooms: 1,
image:
'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg',
},
},
{
type: 'rentals',
id: 'downtown-charm',
attributes: {
title: 'Downtown Charm',
owner: 'Violet Beauregarde',
city: 'Portland',
'property-type': 'Apartment',
bedrooms: 3,
image:
'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg',
},
},
],
};
});
}
Run Code Online (Sandbox Code Playgroud)
您看到它的原因是,从 v4 Ember 开始,不允许最近出现的一些隐式服务注入。路线中的商店注入就是其中之一。它首先是在 3.26 中作为弃用添加的,但现在从 v4 开始它已被删除,显然他们还没有更新文档。
你应该做的是显式地注入它,即在你的app/route/rental.jsmake中
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class RentalsRoute extends Route {
@service store;
model() {
return this.store.findAll('rental');
}
}
Run Code Online (Sandbox Code Playgroud)