首页 > 解决方案 > 无法获取空的用户表单

问题描述

我的代码仅适用于使用编辑按钮加载数据,我可以更新数据!!!但是当我单击菜单中的 AddNew 车辆选项以添加新数据时,我得到的表单中的加载值为上次编辑。没有得到空表单来添加新数据。

如果我没有点击编辑按钮,那么菜单中的 AddNew 车辆选项来添加新数据,我会得到 Empty 表单来添加新数据。

我该如何解决这个问题。

我的代码位于:https ://stackblitz.com/edit/angular-rlf3nz

标签: angular

解决方案


问题是您正在重定向到同一条路线。因此角度防止它。

只需将这么多代码放入user-form.components.ts要导航到的组件 ( ) 的构造函数中。

this.router.routeReuseStrategy.shouldReuseRoute = function () {
  return false;
};

this.router.events.subscribe((evt) => {
    this.router.navigated = false;      
});

另外,为菜单点击添加一个回调函数:

   items: [
        { 
          label: 'Add New Vehicle', icon: 'fa fa-refresh',  
          command: () => {
          let user = new User();
          this._userService.setter(user);
          this.router.navigate(['/op']);  
          }  
        },
        { label: 'Show All', icon: 'fa fa-repeat', url: '#' }
      ]

工作代码链接: https ://stackblitz.com/edit/angular-xjhvtj?file=src%2Fapp%2Fapp.component.ts 工作示例链接: https ://angular-xjhvtj.stackblitz.io


推荐阅读