逗人, 我知道这个问题是重复的,但实际上我尝试了所有可用的解决方案,但仍无法解决我的问题。 我有多个组件和服务我将服务导入到signin,signup等组件。 但我有一个组件可以解决问题,如果我从中删除了服务注入应用程序将运行完美。
无效组件是:
Activation.ts
import {Component,Injectable,Inject} from '@angular/core';
import {CloudAuth} from '../../shared/auth_service';
@Component({
selector: 'page-activation',
templateUrl: 'activation.html'
})
export class Activation {
constructor( @Inject(CloudAuth) public _CloudAuth:CloudAuth ){
}
}
其中一个工作组件login.ts
import {Component} from '@angular/core';
import {NavController, NavParams, LoadingController, AlertController, AlertOptions, Platform} from 'ionic-angular';
import {FormBuilder, Validators, FormGroup} from '@angular/forms';
import {TranslateService} from "ng2-translate";
import {Facebook} from 'ionic-native';
import {CloudAuth} from '../../shared/auth_service';
@Component({
selector:'page-login',
templateUrl: 'login.html',
})
export class login {
constructor(public loading: LoadingController, public navCtrl: NavController, public params: NavParams,
public _userService: UserService, public _FormBuilder: FormBuilder, public translate: TranslateService,
public alertCtrl: AlertController , public _CloudAuth:CloudAuth) {
...........
}
...........
}
我的服务代码是auth_service.ts:
import {Injectable , Inject} from "@angular/core";
import { Platform } from 'ionic-angular';
import "rxjs/add/operator/map";
import { JwtHelper } from 'angular2-jwt';
import {TranslateService} from 'ng2-translate/ng2-translate';
import {Activation} from '../user/activation/activation';
import {welcomePage} from '../welcome/welcome';
import {userLogin} from '../user/user';
import {CompleteRegistration} from '../user/completeRegistration/completeRegistration';
import {HomePage} from '../home/home';
@Injectable()
export class CloudAuth {
jwtHelper: JwtHelper = new JwtHelper();
token=null;
constructor(
@Inject(TranslateService) public translate: TranslateService ,
@Inject(Platform) public platform: Platform
){
this.token = localStorage.getItem('user_token');
}
getToken(){
return this.token;
}
updateToken(newtoken){
localStorage.setItem('user_token', newtoken.toString());
this.token = newtoken;
return true;
}
isTokenExpired(){
// Check if token expired
return this.jwtHelper.isTokenExpired(this.token)
}
tokenData(){
// Return data stored in token
if(this.token=='' || this.token == null) return false;
else return this.jwtHelper.decodeToken(this.token);
}
checkCode(code){
let userData = this.tokenData();
if( userData == false) return false;
else {
if(userData.activation_code == code) return true;
else return false;
}
}
// Check if user login or not and loading the default app Language.
checkLogin():any{
console.log(this.token);
let lang = localStorage.getItem('Language');
if(!lang || lang==''|| lang === undefined ) return welcomePage;
else {
if(lang=='ar'){
this.platform.setLang('ar',true);
this.platform.setDir('rtl',true);
}
this.translate.use(lang);
let userData = this.tokenData();
if( this.token==null || this.token== '' || userData == false) return userLogin;
else {
if(userData.active == 1) {
if(!localStorage.getItem('firstRun') ){
return CompleteRegistration;
}else{
return HomePage;
}
}else {
return Activation;
}
}
}
}
}
我删除了@Inject装饰器但也没有工作。
我的package.json是:
{
"name": "ionic-hello-world",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
},
"dependencies": {
"@angular/common": "2.2.1",
"@angular/compiler": "2.2.1",
"@angular/compiler-cli": "2.2.1",
"@angular/core": "2.2.1",
"@angular/forms": "2.2.1",
"@angular/http": "2.2.1",
"@angular/platform-browser": "2.2.1",
"@angular/platform-browser-dynamic": "2.2.1",
"@angular/platform-server": "2.2.1",
"@ionic/storage": "1.1.6",
"angular2-jwt": "^0.1.25",
"ionic-angular": "2.0.0-rc.4",
"ionic-native": "2.2.11",
"ionicons": "3.0.0",
"ng2-translate": "^4.0.1",
"rxjs": "5.0.0-beta.12",
"zone.js": "0.6.26"
},
"devDependencies": {
"@ionic/app-scripts": "0.0.47",
"typescript": "2.0.9"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-statusbar",
"cordova-plugin-console",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": [
"ios",
{
"platform": "ios",
"version": "",
"locator": "ios"
}
],
"description": "pecardPlayground: An Ionic project"
}
我的节点和离子信息是:
Ionic Framework: 2.0.0-rc.4
Ionic Native: 2.2.11
Ionic App Scripts: 0.0.47
Angular Core: 2.2.1
Angular Compiler CLI: 2.2.1
Node: 6.7.0
OS Platform: macOS Sierra
此错误在升级到Ionic RC4之前不会出现 希望找到解决方案。 感谢
答案 0 :(得分:2)
您似乎有一个循环依赖的案例。
您已导入Activation
中的auth_service
组件并已导入
auth_service
中的Activation
是您的起始页。
使用forwardRef
constructor(@Inject(forwardRef(() => CloudAuth)) _CloudAuth)
在激活中的构造函数中。 另一个教程链接here
编辑: 如果可能,也可以重新考虑设计。