在Ionic2 / Angular2应用程序中使用Angular1服务:没有服务提供者

时间:2016-07-25 13:40:37

标签: angular dependency-injection ionic2 bootstrapping

我正在尝试使用我的一些Ionic1 Angular服务创建新的Ionic2应用程序。我已阅读https://angular.io/docs/ts/latest/guide/upgrade.html并知道我应该使用UpgradeAdapter但由于某种原因它不起作用。

这是我的app.ts文件引导Ionic2应用程序:

// This is to tell TS that `require` method is in fact available
declare function require(name:string);

import { Component, Inject } from '@angular/core';
import { ionicBootstrap } from 'ionic-angular';
import { HTTP_PROVIDERS } from '@angular/http';

let angular = require('angular');

import { UpgradeAdapter } from '@angular/upgrade';
let adapter = new UpgradeAdapter();

angular.module('myAngular1App', [])
     .service('MyAngular1Service', function() {
        return {
            hello: function() {
                console.log('Hello!')
            }
        }
    });

adapter.bootstrap(document.body, ['myAngular1App']);

adapter.upgradeNg1Provider('testService');

@Component({
    templateUrl: 'build/app.html'
})
class MyAngular2App {
    constructor(@Inject('testService') testService) {
        console.log('construct');
        testService.hello();
    }
}

ionicBootstrap(MyAngular2App, [
    HTTP_PROVIDERS
]);

如果没有Angular1 testService注入,它可以正常工作,但有了它我会看到DI异常 No provider for testService!

enter image description here

我在这里缺少什么?

0 个答案:

没有答案