简体   繁体   English

ember 错误 this.store.findAll 不是 function

[英]ember error this.store.findAll is not a function

hey all im going through the emberjs tutorial and i've run into this issue that i'm unable to resolve.嘿,我正在阅读 emberjs 教程,我遇到了这个我无法解决的问题。 when i try to call the findAll function from store it throws a typeerror and says that findAll is not a function.当我尝试从存储中调用 findAll function 时,它会引发类型错误并说 findAll 不是 function。 when i use The this.get() method it says itss a classic ember object method, and can't be used in octane classes.当我使用 this.get() 方法时,它说它是经典的 ember object 方法,不能用于辛烷值类。 does anybody have any idea how to fix this?有人知道如何解决这个问题吗?

thanks in advance for your time!在此先感谢您的时间!

这是控制台错误消息

app/route/rental.js应用程序/路由/rental.js

import Route from '@ember/routing/route';

export default class RentalsRoute extends Route {
  model() {
    return this.store.findAll('rental');
  }
}

app/models/rental.js应用程序/模型/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;
}

mirage/config.js海市蜃楼/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',
          },
        },
      ],
    };
  });
}

The reason why you see it is that starting from the v4 Ember doesn't allow some implicit service injections which were there up recently.您看到它的原因是从 v4 Ember 开始不允许一些最近出现的隐式服务注入。 The store injection in routes was one of them.路线中的商店注入就是其中之一。 It was first added as a deprecation in 3.26 but now as of v4 it's removed and apparently they haven't updated the documentation.它最初是 在 3.26 中作为弃用添加的,但现在从 v4 开始,它已被删除,显然他们还没有更新文档。

What you should do is to inject it explicitly, ie in your app/route/rental.js make你应该做的是显式注入它,即在你的app/route/rental.js

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');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM