Angular 6 + Routes解析器= 404页在生产中

时间:2019-01-11 17:25:59

标签: javascript angular angular-ui-router angular6 angular-routerlink

我是来自Rails的Angular社区的新手;我正在一个网站上发布我的客户网站的案例研究。到目前为止,我喜欢棱角分明,但遇到一个让我有些担心的问题。我似乎无法使用超链接引用任何项目的问题。例如,如果您单击 this ,该应用程序将返回404状态,但是如果您从 root page 开始,则一切都将没有问题。< / p>

我尝试过的解决方案

  1. 在路由中添加解析器以获取数据,然后再加载 页;它可以工作,但仍然没有超链接。

任何人都可以帮助我找出我所缺少或做错的事吗

下面是我的代码;任何帮助将不胜感激

  1. Project.service.ts

    import { AngularFirestore } from 'angularfire2/firestore';
    import { Observable, of } from 'rxjs';
    import { map,  } from 'rxjs/operators';
    import { Injectable } from '@angular/core';
    import { Project } from '../../models/project';
    import { HttpClient } from '@angular/common/http';
    import { Post } from '../post';
    
    
    
    
    @Injectable()
    
    export class ProjectService {
    
        ProjectListRef: Observable<any[]>;
        featuredProjectList: Observable<any[]>;
        posts: Array<Post>;
   
    
        constructor( private database: AngularFirestore, private http: HttpClient) {
            // Link For All Projects Page
            const allProjectsListLink = this.database.collection<Project>('projects', ref => ref.orderBy('company_name'));
    
    
    
            // Link For Featured Project Page
            const featuredProjectsListLink = this.database.collection<Project>('projects', ref => ref.limit(5));
    
    
    
this.ProjectListRef = allProjectsListLink.snapshotChanges()
            // using map pipe
            .pipe(map( actions => {
                return actions.map(a => {
                    // Get Document Data
                    const data = a.payload.doc.data() as Project;

                    // Get Document Id

                    const id = a.payload.doc.id;

                    return {id, ...data};

                });
            }));
    
      this.featuredProjectList = featuredProjectsListLink.snapshotChanges()
      // using map pipe
      .pipe(map( actions => {
      return actions.map(a => {
      // Get Document Data
      const data = a.payload.doc.data() as Project;

      // Get Document Id

      const id = a.payload.doc.id;

      return {id, ...data};

      });
      }));


      }


        getAllProjects() {
            return this.ProjectListRef;
        }
    
        getFeaturedProjects() {
            return this.featuredProjectList;
        }
    
        getProject(id: string) {
            console.log(id);
            return this.database.collection('projects').doc(id)
            .ref.get().then( doc => doc.data() );
          }
    
    }

  1. Project.component.ts

        import { Component, OnInit, Injectable, Inject } from '@angular/core';
                import { Router, ActivatedRoute } from '@angular/router';
                import {  tap } from 'rxjs/operators';
                import { ProjectService } from '../../app/services/project.service';
                import { AngularFirestore } from 'angularfire2/firestore';
                import { Project } from '../../models/project';
                import { DOCUMENT } from '@angular/common';
    
            @Injectable()
            
            @Component({
              selector: 'app-project-page',
              templateUrl: './project-page.component.html',
              styleUrls: ['./project-page.component.scss']
            })
            export class ProjectPageComponent implements OnInit {
            
              project: Project;
              projectId: any;
              projectRef: any;
            
            
              constructor(@Inject(DOCUMENT) private document: any, private router: Router,
                          private route: ActivatedRoute,
                          public database: AngularFirestore,
                          private projectService: ProjectService
                        ) {
            
                  }
            
            
              ngOnInit() {
            
                  // print out the data from the route resolver
                  this.route.data.pipe(
                    tap(console.log))
                    .subscribe(
                      data => this.project = data['project']
                  );
            
              }
            
              goToWebsite(url) {
                this.document.location.href = url;
              }
            
            
            
            }

 3. **ProjectResolver.ts**

import { ProjectService } from './../../app/services/project.service';
import { Project } from './../project';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
    
    
    @Injectable()
    
    export class ProjectResolver implements Resolve<Project> {
    
        constructor(private projectService: ProjectService) {
    
        }
    
        resolve(
                route: ActivatedRouteSnapshot,
                state: RouterStateSnapshot): Observable<Project>|Promise<any> {
                return this.projectService.getProject(route.params['id']);
                }
    
    
    }

  1. 申请路线

import { AddLeadPageComponent } from './add-lead-page/add-lead-page.component';
import { GoogleAdsPageComponent } from './google-ads-page/google-ads-page.component';
import { WebAppsPageComponent } from './web-apps-page/web-apps-page.component';
import { BrandedWebsitesPageComponent } from './branded-websites-page/branded-websites-page.component';
import { ServiceOverviewPageComponent } from './service-overview-page/service-overview-page.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomePageComponent } from './home-page/home-page.component';
import { AboutPageComponent } from './about-page/about-page.component';
import { BlogPageComponent } from './blog-page/blog-page.component';
import { WebServicesPageComponent } from './web-services-page/web-services-page.component';
import { SEMPageComponent } from './sempage/sempage.component';
import { ContactPageComponent } from './contact-page/contact-page.component';
import { AllProjectsPageComponent } from './all-projects-page/all-projects-page.component';
import { ResearchServicePageComponent } from './research-service-page/research-service-page.component';
import { ProjectPageComponent } from './project-page/project-page.component';
import { TeamPageComponent } from './team-page/team-page.component';
import { NewProjectComponent } from './new-project/new-project.component';
import { DigitalMarketingPageComponent } from './digital-marketing-page/digital-marketing-page.component';
import { ProjectResolver } from '../models/resolvers/project.resolver';

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomePageComponent},
  { path: 'about', component: AboutPageComponent},
  { path: 'blog', component: BlogPageComponent},
  { path: 'team', component: TeamPageComponent},
  { path: 'portfolio', component: AllProjectsPageComponent},
  { path: 'services', component: ServiceOverviewPageComponent},
  { path: 'project/:id', component: ProjectPageComponent, resolve: { project: ProjectResolver}},
  { path: 'brand-strategy', component: ResearchServicePageComponent},
  { path: 'digital-strategy', component: DigitalMarketingPageComponent},
  { path: 'custom-websites', component: BrandedWebsitesPageComponent},
  { path: 'web-apps', component: WebAppsPageComponent},
  { path: 'google-ads', component: GoogleAdsPageComponent},
  { path: 'web-design-development', component: WebServicesPageComponent},
  { path: 'search-engine-marketing', component: SEMPageComponent},
  { path: 'start-new-project', component: ContactPageComponent},
  { path: 'new-project', component: NewProjectComponent},
// post trial
  { path: 'posts/add', component: AddLeadPageComponent},
  { path: 'posts/add/:id', component: AddLeadPageComponent},
];

@NgModule({
  imports: [
            RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled'})
            ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

1 个答案:

答案 0 :(得分:0)

您的角度代码没有任何错误。您面临的问题是网址没有被重写为index.html。您需要在Firebase设置(firebase.json)中进行配置。这是最简单,最常见的配置:

  "hosting": {
    "public": "dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}