Javascript 没有视图的Aurelia模型在注销时返回错误

Javascript 没有视图的Aurelia模型在注销时返回错误,javascript,ecmascript-6,aurelia,Javascript,Ecmascript 6,Aurelia,我是Aurelia社区的新成员,我的任务是对当前平台进行全面升级。(更多信息在底部) 当前问题: 每次我重定向到logout.js模型时,都会提示一条消息 错误[app router]类型错误:“this.view为空” 问题: 自定义组件“如果允许”如何影响非视图模型 结论: -我开始相信下面的任何大文件都会影响错误!在注释了大部分代码后,仍然显示错误! -删除了noView()逻辑并添加了一个空logout.html!你猜怎么着?工作起来很有魅力!注销将重定向到登录页面 这是我的RouteC

我是Aurelia社区的新成员,我的任务是对当前平台进行全面升级。(更多信息在底部)

当前问题: 每次我重定向到logout.js模型时,都会提示一条消息

错误[app router]类型错误:“this.view为空”

问题: 自定义组件“如果允许”如何影响非视图模型

结论: -我开始相信下面的任何大文件都会影响错误!在注释了大部分代码后,仍然显示错误! -删除了noView()逻辑并添加了一个空logout.html!你猜怎么着?工作起来很有魅力!注销将重定向到登录页面

这是我的RouteConfig.js

    {
      route: 'logout',
      viewPorts: {
        main: {
          moduleId: PLATFORM.moduleName('pages/logout/logout')
        }
      },
      nav: false,
      sidebar: false,
      auth: false,
      title: 'Logout',
      name: 'logout',
    }
这是我的logout.js

import { noView } from 'aurelia-framework';
import authService from 'services/authService';
import uiService from 'services/uiService';

@noView()
export class LogoutPage {
  activate() {
    //THE ERROR PROMPTS EVEN WITH THE ABOVE LINES COMMENTED
    uiService.impersonate(null, false);
    authService.logout();
  }
}
搜索一段时间后,我注意到在这2个文件上声明了“this.view”:

if-permission.js

import { inject, customAttribute, templateController, BoundViewFactory, ViewSlot } from 'aurelia-framework';
import userService from 'services/api/userService';

@customAttribute('if-permission')
@inject(BoundViewFactory, ViewSlot)
@templateController
export class IfPermission {

  constructor(viewFactory, viewSlot) {

    this.viewFactory = viewFactory;
    this.viewSlot = viewSlot;
    this.showing = false;
    this.view = null;
    this.bindingContext = null;
    this.overrideContext = null;
  }

  /**
  * Binds the if to the binding context and override context
  * @param bindingContext The binding context
  * @param overrideContext An override context for binding.
  */
  bind(bindingContext, overrideContext) {
    // Store parent bindingContext, so we can pass it down
    this.bindingContext = bindingContext;
    this.overrideContext = overrideContext;
    this.valueChanged(this.value);
  }

  valueChanged(newValue) {
    if (this.__queuedChanges) {
      this.__queuedChanges.push(newValue);
      return;
    }

    let maybePromise = this._runValueChanged(newValue);
    if (maybePromise instanceof Promise) {
      let queuedChanges = this.__queuedChanges = [];

      let runQueuedChanges = () => {
        if (!queuedChanges.length) {
          this.__queuedChanges = undefined;
          return;
        }

        let nextPromise = this._runValueChanged(queuedChanges.shift()) || Promise.resolve();
        nextPromise.then(runQueuedChanges);
      };

      maybePromise.then(runQueuedChanges);
    }
  }

  _runValueChanged(newValue) {
    newValue = userService.hasPermission(newValue);
    if (!newValue) {
      let viewOrPromise;
      if (this.view !== null && this.showing) {
        viewOrPromise = this.viewSlot.remove(this.view);
        if (viewOrPromise instanceof Promise) {
          viewOrPromise.then(() => this.view.unbind());
        } else {
          this.view.unbind();
        }
      }
      this.showing = false;
      return viewOrPromise;
    }

    if (this.view === null) {
      this.view = this.viewFactory.create();
    }

    if (!this.view.isBound) {
      this.view.bind(this.bindingContext, this.overrideContext);
    }

    if (!this.showing) {
      this.showing = true;
      return this.viewSlot.add(this.view);
    }
  }

  /**
  * Unbinds the if
  */
  unbind() {
    if (this.view === null) {
      return;
    }

    this.view.unbind();

    if (!this.viewFactory.isCaching) {
      return;
    }

    if (this.showing) {
      this.showing = false;
      this.viewSlot.remove(this.view, true, true);
    }
    this.view.returnToCache();
    this.view = null;
  }
}
if-user-role.js

import { inject, customAttribute, templateController, BoundViewFactory, ViewSlot } from 'aurelia-framework';
import userService from 'services/api/userService';

@customAttribute('if-user-role')
@inject(BoundViewFactory, ViewSlot)
@templateController
export class IfUserRole {

  constructor(viewFactory, viewSlot) {
    this.viewFactory = viewFactory;
    this.viewSlot = viewSlot;
    this.showing = false;
    this.view = null;
    this.bindingContext = null;
    this.overrideContext = null;
  }

  /**
  * Binds the if to the binding context and override context
  * @param bindingContext The binding context
  * @param overrideContext An override context for binding.
  */
  bind(bindingContext, overrideContext) {
    // Store parent bindingContext, so we can pass it down
    this.bindingContext = bindingContext;
    this.overrideContext = overrideContext;
    this.valueChanged(this.value);
  }

  valueChanged(newValue) {
    if (this.__queuedChanges) {
      this.__queuedChanges.push(newValue);
      return;
    }

    let maybePromise = this._runValueChanged(newValue);
    if (maybePromise instanceof Promise) {
      let queuedChanges = this.__queuedChanges = [];

      let runQueuedChanges = () => {
        if (!queuedChanges.length) {
          this.__queuedChanges = undefined;
          return;
        }

        let nextPromise = this._runValueChanged(queuedChanges.shift()) || Promise.resolve();
        nextPromise.then(runQueuedChanges);
      };

      maybePromise.then(runQueuedChanges);
    }
  }

  _runValueChanged(newValue) {
    newValue = userService.hasRole(newValue);

    if (!newValue) {
      let viewOrPromise;
      if (this.view !== null && this.showing) {
        viewOrPromise = this.viewSlot.remove(this.view);
        if (viewOrPromise instanceof Promise) {
          viewOrPromise.then(() => this.view.unbind());
        } else {
          this.view.unbind();
        }
      }

      this.showing = false;
      return viewOrPromise;
    }

    if (this.view === null) {
      this.view = this.viewFactory.create();
    }

    if (!this.view.isBound) {
      this.view.bind(this.bindingContext, this.overrideContext);
    }

    if (!this.showing) {
      this.showing = true;
      return this.viewSlot.add(this.view);
    }
  }

  /**
  * Unbinds the if
  */
  unbind() {
    if (this.view === null) {
      return;
    }

    this.view.unbind();
    if (!this.viewFactory.isCaching) {
      return;
    }

    if (this.showing) {
      this.showing = false;
      this.viewSlot.remove(this.view, true, true);
    }
    this.view.returnToCache();
    this.view = null;
  }
}
通过这次更新,我集成了Aurelia cli,更新了Aurelia webpack和所有依赖项。这让我切换了一些代码,比如:

  • 将PLATFORM.moduleName()添加到所有我的平台
  • 将Require添加到仅通过获取组件的所有模块中

上面代码块中的哪一行抛出错误?在不知道的情况下很难猜出发生了什么。好奇的是,你知道为什么会这样做吗?代码来自以前版本的旧
If
实现。错误显示在If-permission.js上(只是猜测,因为坚持通过console.log进行调试),Aurelia控制台日志没有多大帮助。但是我可以尝试复制一个粘贴所有错误声明。不,我觉得我不知道为什么这样做,因为我是Aurelia的新手,只是在一个预构建的平台上开发。谢谢您的时间!我已经编辑了这个帖子!去看看我的结论:)很高兴看到你把它解决了。