我正在尝试转换 Angular 2+ 服务并在 angularJs 项目中使用它。
应用程序/users.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class UsersService {
private users: any = [
{ id: 1, name: 'john' },
{ id: 2, name: 'jane' }
];
constructor() { }
getUsers() {
return this.users;
}
}
然后我用 rollup 把它转换成 js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/upgrade/static'), require('@angular/platform-browser')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/upgrade/static', '@angular/platform-browser'], factory) :
(factory((global.mymodule = {}),global.ng.core,global._static,global.platformBrowser));
}(this, (function (exports,core,_static,platformBrowser) { 'use strict';
var UsersService = /** @class */ (function () {
function UsersService() {
this.users = [
{ id: 1, name: 'john' },
{ id: 2, name: 'jane' }
];
}
UsersService.prototype.getUsers = function () {
return this.users;
};
UsersService.decorators = [
{ type: core.Injectable },
];
/** @nocollapse */
UsersService.ctorParameters = function () { return []; };
return UsersService;
}());
var MyModule = /** @class */ (function () {
function MyModule(upgrade) {
this.upgrade = upgrade;
}
MyModule.decorators = [
{ type: core.NgModule, args: [{
imports: [platformBrowser.BrowserModule, _static.UpgradeModule],
declarations: [],
providers: [UsersService],
exports: []
},] },
];
/** @nocollapse */
MyModule.ctorParameters = function () { return [
{ type: _static.UpgradeModule, },
]; };
return MyModule;
}());
exports.UsersService = UsersService;
exports.MyModule = MyModule;
Object.defineProperty(exports, '__esModule', { value: true });
})));
我添加 downgradeInjectable 将 angular2+ 服务转换为 angularJs
angular.module('users.service', [])
.factory('usersServices', downgradeInjectable(usersServices));
接下来我尝试在angularJs中加载它
(function() {
'use strict';
angular.module(
'ng1.module', [
'usersServices'
]);
})();
我错过了一些东西,因为它不起作用:/
我找不到资源来做这件事,所有人都试图将 ng1 服务转换为 ng2,但不是相反。
有人可以帮忙吗?
谢谢