javascript - Ember Octane Route 类是否支持使用 mixins?

标签 javascript ember.js

我正在升级到 Ember Octane,并且我了解到 mixin 已被弃用。我将继续使用它们,直到我弄清楚如何更换它们。与此同时,我想将我的路由切换为使用新的类语法,而不是 Route.extend。新的路由类语法是否支持路由混合?如果是,怎么办?

这与 Ember Octane Upgrade How to pass values from component to controller 相关

Ember 前辛烷值:

import Route from '@ember/routing/route';
import AbcAuthenticatedRouteMixin from '../../mixins/abc-authenticated-route-mixin';

export default Route.extend(AbcAuthenticatedRouteMixin, {

    model() {

        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
        };
    },
})

Ember 后辛烷值:

import Route from '@ember/routing/route';
import AbcAuthenticatedRouteMixin from '../../mixins/abc-authenticated-route-mixin';

export default class ChangePasswordRoute extends Route(AbcAuthenticatedRouteMixin, {

    model() {

        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
        };
    },
}) // I get an error here that says: '{' expected

最佳答案

原生类语法并不直接具有 Ember mixin 系统的等效语法。如果您想在转换为 Octane 时继续使用 mixin,可以通过将经典类扩展语法与 native 类语法混合来实现:

尝试

import Route from '@ember/routing/route';
import AbcAuthenticatedRouteMixin from '../../mixins/abc-authenticated-route-mixin';

export default class ChangePasswordRoute extends Route.extend(AbcAuthenticatedRouteMixin) {

    model() {

        return {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
        };
    }
}

此外,一些新的框架类,例如 Glimmer 组件,根本不支持 Ember mixin。 future mixins将会从框架中移除,不会直接替换。对于使用 mixins 的应用程序,建议的路径是将 mixins 重构为其他模式,包括:

Pure native classes, sharing functionality via class inheritance. Utility functions which can be imported and used in multiple classes. Services which can be injected into multiple classes, sharing functionality and state between them.

关于javascript - Ember Octane Route 类是否支持使用 mixins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60843983/

相关文章:

Javascript:五彩纸屑颗粒落在 Canvas 上

javascript - 使用 passport.js、mysql 和 express-session 保持登录状态

javascript - 如何在文件上传完成时调用 JS 回调?

javascript - 使固定的 float 元素在 Firefox 和 Chrome 中平滑滚动

jquery-plugins - Ember 数据日期绑定(bind)

javascript - 选择选项元素上的单击事件不起作用

jquery - ember.js - 可以使用 jQuery 2.x 吗?

javascript - Ember.js:如何在选择帮助器上使用观察者来过滤内容?

html - Ember.js 应用程序中的 Material design lite 输入在路由转换后丢失了它的设计

ember.js - MODEL_FACTORY_INJECTIONS 和夹具