Angular 5重新加载并构建错误

时间:2018-05-04 02:55:10

标签: angular typescript angular-routing angular-router

这里只有几个问题。

一,当我使用浏览器刷新时,它会重新加载回/而不是刷新前加载的路由。以下是我的路线定义。

const appRoutes: Routes = [
  { path: '', component: LandingModule, canActivate: [AuthGuardService] },
  { path: 'login', component: LoginModule, canActivate: [AuthGuardService] },
  { path: 'register', component: RegisterModule, canActivate: [AuthGuardService] },
  { path: 'dashboard', component: DashboardModule, canActivate: [AuthGuardService] },
  { 
    path: 'vendors', 
    component: VendorModule,
    canActivate: [AuthGuardService],
    children: [
      { path: 'new', component: NewVendorModule, canActivate: [AuthGuardService] }
    ]
  }
]

其次,当我尝试运行ng build或只是刷新页面时,我会收到这些错误。我该如何解决这些问题?

错误:

src/app/app.component.ts(33,14): error TS2339: Property 'companies' does not exist on type '{}'.

参考代码块

sysUser.companies.forEach(function(company){
    if(company.default){
       defaultComp = company;
    }
})

错误

src/app/services/users/users.service.ts(23,21): error TS2339: Property 'id' does not exist on type 'void'.

参考代码块

createRootUser(uid: string, company: string, companyName: string) {
    return new Promise((resolve, reject) => {
        firebase.firestore().collection("users").doc(uid).set({
            'companies' : [
          {
            id : company,
            name : companyName
          }
        ]
        }).then(docRef => {
            resolve(docRef.id);
        }).catch(error => {
            reject(error);
        })
    })
  }

更新 共享APP.COMPONENT.TS

import { Component } from '@angular/core';
import { Router } from '@angular/router';

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore } from 'angularfire2/firestore';
import * as firebase from 'firebase/app';

import { UsersService } from './services/users/users.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

    public firebaseUser : any;
    public systemUser: any;
    public activeCompany: any;

    constructor(public userServ: UsersService, public afAuth: AngularFireAuth, public afStor: AngularFirestore, private router: Router) {

        const settings = {timestampsInSnapshots: true};
        firebase.firestore().settings(settings);

        firebase.auth().onAuthStateChanged(user => {
            if(user) {

                this.firebaseUser = user;
                this.userServ.fetchUser(user.uid).then(sysUser => {
                    this.systemUser = sysUser;
                    var defaultComp : any;
                    sysUser.companies.forEach(function(company){
                        if(company.default){
                            defaultComp = company;
                        }
                    })
                    this.activeCompany = defaultComp;
                }).catch(error => {
                    console.log('ERROR:', error);
                })

            } else {
                this.firebaseUser = null;
            }
        });

    }

    public sidebarClosed: boolean = true;
    public sidebarSetOne = true;
    public sidebarSetTwo = true;
    public sidebarSetThree = true;

    logOut(): void {
        firebase.auth().signOut();
    }

}

1 个答案:

答案 0 :(得分:0)

警卫看起来出了问题: AuthGuardService 必须有一些东西将重定向到根URL。如果你要分享AuthGuardService代码会更清楚。

对于app.component.ts中的错误,您可以尝试使用以下更改

  

sysUser.companies.forEach((company:any)=> {   如果(company.default){   defaultComp   =公司; }})