简体   繁体   中英

Angular 2+ services to angularJs

I m trying to convert Angular 2+ service and use it in angularJs project.

app/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;
    }
}

then I tranform it into js with rollup

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

})));

I add downgradeInjectable to convert angular2+ service into angularJs

angular.module('users.service', [])
.factory('usersServices', downgradeInjectable(usersServices));

next I try to load it in angularJs

(function() {
    'use strict';
    angular.module(
        'ng1.module', [
            'usersServices'
        ]);
})();

I m missing something because it doesn't work :/

I can't find ressource to do it, all people a trying to convert ng1 service to ng2 but not the reverse.

Someone can help ?

Thank you

Instead of importing 'usersServices' (the service factory) in the module 'ng1.module', you should import the actual module with 'usersServices' in it instead:

(function() {
    'use strict';
    angular.module(
        'ng1.module', [
            'users.service'
        ]);
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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