简体   繁体   English

我们可以将es6类​​添加到现有的角度1.x ROR应用程序中吗?

[英]can we add es6 classes to an existing angular 1.x ROR app?

I have an existing RoR app which uses angular 1.4.9. 我有一个使用角度1.4.9的现有RoR应用程序。 I want to play around with ES-6 features, I want to build new features using Class in ES6. 我想使用ES-6功能,我想使用ES6中的Class构建新功能。 Is it possible to use define new classes which can be used inside the existing angular app and vice versa. 是否可以使用定义新类,可以在现有的角度应用程序中使用,反之亦然。 I know that you can use transpilers(like babel) and add the js to the rails asset pipeline. 我知道您可以使用转发器(如babel)并将js添加到rails资产管道中。 My question is around how to export/import modules between es6 classes & existing angular app code. 我的问题是如何在es6类和现有的角度应用程序代码之间导出/导入模块。

One approach is to put pure ES6 classes/modules in separate files, eg. 一种方法是将纯ES6类/模块放在单独的文件中,例如。

class UserService {

    // all your beautiful ES6 class stuff goes here ...
}
export {UserService};

And in a separate file import these and and plug them into angular 1.* 并在一个单独的文件中导入这些并将它们插入角度1. *

import 'angular';
import {UserService} from './user-service';

angular.module('myApp', [])
    .service('userService', UserService);

This helps to keep your new ES6 based business logic separate from angular 1.* code which can help ease the upgrade to angular 2.* 这有助于保持新的基于ES6的业务逻辑与角度1. *代码分开,这有助于简化升级到角度2. *

The next step though is to set up a build process which can not only handle the transpiling of the ES6, but that can also handle the ES6 module imports/exports. 下一步是建立一个构建过程,它不仅可以处理ES6的转换,还可以处理ES6模块的导入/导出。 We found jspm ( http://jspm.io/ ) a good option for this. 我们发现jspm( http://jspm.io/ )是一个不错的选择。 There is a getting started guide for this approach here http://legacytotheedge.blogspot.co.nz/2015/01/using-es6-with-your-angularjs-project.html 这里有一个入门指南http://legacytotheedge.blogspot.co.nz/2015/01/using-es6-with-your-angularjs-project.html

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

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