我想从承诺值中获取价值,然后返回canActive布尔值

时间:2019-07-10 04:59:09

标签: angular rxjs

我试图从使用路由器防护与路径的值中进行评估

我试图用来将承诺中的值放入import UIKit import SnapKit @IBDesignable class FlightStopsView: UIView { var startPoint: UILabel! var endPoint: UILabel! var line: UIView! var stopsStack: UIStackView! var stopCount: UILabel! var stops: [Stop]! { didSet { addStops() setNeedsLayout() setNeedsDisplay() } } @IBInspectable var lineColor: UIColor! { didSet{ line?.backgroundColor = UIColor.lightGray layoutIfNeeded() } } override init(frame: CGRect) { super.init(frame: frame) setUpView() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) //Here is the change: Make sure you setupview in case of you are calling it from Storyboard setUpView() } func setUpView() { startPoint = UILabel() startPoint.text = "0" self.addSubview(startPoint) startPoint.snp.makeConstraints { make in make.width.equalTo(10) make.height.equalTo(10) make.leading.equalToSuperview() make.centerY.equalToSuperview() } endPoint = UILabel() endPoint.text = "0" self.addSubview(endPoint) endPoint.snp.makeConstraints { make in make.width.equalTo(10) make.height.equalTo(10) make.trailing.equalToSuperview() make.centerY.equalToSuperview() } line = UIView() line.backgroundColor = UIColor.lightGray self.addSubview(line) line.snp.makeConstraints { make in make.height.equalTo(1) make.centerY.equalToSuperview() make.leading.equalTo(startPoint.snp.trailing).offset(-1) make.trailing.equalTo(endPoint.snp.leading).offset(1) } stopCount = UILabel() stopCount.textColor = UIColor.lightGray stopCount.font = UIFont.regularFont(Dimens.FontSize.SMALL) stopCount.adjustsFontSizeToFitWidth = true stopCount.text = "Non Stop" self.addSubview(stopCount) stopCount.snp.makeConstraints { make in make.centerX.equalToSuperview() make.top.equalTo(line.snp.bottom).offset(4) make.bottom.equalToSuperview() } stopsStack = UIStackView() stopsStack.backgroundColor = .clear stopsStack.alignment = .center stopsStack.distribution = .fill stopsStack.contentMode = .center stopsStack.spacing = 30 self.addSubview(stopsStack) stopsStack.snp.makeConstraints{ make in make.centerX.equalToSuperview() make.top.equalToSuperview() make.bottom.equalTo(line.snp.bottom).offset(4.5) } addStops() } func addStops() { guard stopCount != nil && stopsStack != nil else { return } guard stops != nil else { stopCount.text = "Non stop" return } stopCount.text = "\(stops.count) stops" for stop in stops { let stackChild = UIView() let stackChildLabel = UILabel() let stackChildPointer = UILabel() stackChildLabel.text = stop.name stackChildLabel.textAlignment = .center stackChildLabel.textColor = UIColor.lightGray stackChildLabel.font = UIFont.regularFont(Dimens.FontSize.SMALL) stackChildLabel.adjustsFontSizeToFitWidth = true stackChildLabel.numberOfLines = 2 stackChildPointer.text = "0" stackChild.addSubview(stackChildLabel) stackChild.addSubview(stackChildPointer) //Here is the change: Although you haven't added your `stackChild` to your main `stopsStack` stopsStack.addSubview(stackChild) stackChild.snp.makeConstraints { make in make.width.equalTo(24) make.height.equalToSuperview() } stackChildPointer.snp.makeConstraints { make in make.width.equalTo(10) make.height.equalTo(10) make.bottom.equalToSuperview() make.centerX.equalToSuperview() } stackChildLabel.snp.makeConstraints{ make in make.width.equalTo(20) make.top.equalToSuperview() make.bottom.equalTo(stackChildPointer.snp.top) make.centerX.equalToSuperview() } stopsStack.addArrangedSubview(stackChild) } } } 中作为路由器保护。

this.user = {
  branch: '',
  department: '',
  designation: '',
  mobile: '+91 ',
}
this.$validator.reset()
this.errors.clear();

如果有可能从价值中获得承诺,则路由器防护可以检查带有路由器路径的路径。

canActivate()是菜单列表数组。并与路由器的navigationEnd路径进行比较。

然后,如果与此数组和navigationEnd不同,则返回true或false。

然后canActivate将其用于检查。这是我的路由器防护方案

1 个答案:

答案 0 :(得分:0)

canActivate可以同时返回booleanPromise<boolean>Observable<boolean>。就您而言,canActivate可以返回可观察承诺

此示例过滤器 NavigationEnd 并返回true

canActivate() {
    return this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map((event: NavigationEnd) => true),
    )
  }

文档:canActivate