在angular 2 RC5路由器3.0.0 RC1中,我想从路由器检查意义的同一组件(或至少相同的文件)中调用canDeactivate
函数:
{ path: 'componentPath', component: MyComponent, canDeactivate: [CanDeactivateGuard] }
CanDeactivateGuard
与MyComponent
位于同一文件中,或者更好,CanDeactivateGuard
可以是MyComponent
,canDeactivate()
函数位于MyComponent
内{ path: 'componentPath', component: MyComponent, canDeactivate: [MyComponent] }
1}}(但我不确定它是否可能)
我尝试过以下路线:
MyComponent
在import {Component, OnInit} from '@angular/core';
import { CanDeactivate } from '@angular/router';
@Component({
selector: 'my-component',
providers: [],
templateUrl: 'MyComponent.html',
directives: []
})
export interface CanDeactivateComponent {
canDeactivate: () => boolean | Observable<boolean>;
}
export class MyComponent implements OnInit, CanDeactivate<CanDeactivateComponent> {
onInit(){
// MyComponent initialization
}
canDeactivate(component: CanDeactivateComponent): Observable<boolean> | boolean {
console.log('test');
return true;
}
}
:
index:2 Error: Error: Can't resolve all parameters for MyComponent: (?, ?)
但我收到以下错误:
public static void pushFCMNotification(String userDeviceIdKey) throws Exception{
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", "Notificatoin Title"); // Notification title
info.put("body", "Hello Test notification"); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + json);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
/*OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();*/
}
答案 0 :(得分:0)
您可以从&#39; @ angular / router&#39;
实施CanDeactivatecanDeactivate(component: MyComponente, route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return true;
}
如果你这样做,我认为你不需要路由器配置中的canDeactivate。
重要的编辑:这是RC5,我不知道它是否也适用于RC1
答案 1 :(得分:0)
我知道这很耗时,但仍然升级到RC.5并使用路线防护。我在这个博客(它使用RC4)中描述了它们:https://yakovfain.com/2016/07/20/angular-2-guarding-routes/
来自该博客的RC.5版本的代码示例如下: https://github.com/Farata/angular2typescript/tree/master/chapter3/router_samples/app