简体   繁体   English

Angular6中的REST API和管理Web应用程序

[英]REST API along with Admin web app in angular6

I have fully function Admin panel made using angular6. 我具有使用angular6制作的功能齐全的管理面板。 Following is a sample coding 以下是示例代码

export const AppRoutes: Routes = [
    {
        path: 'login',
        component: LoginComponent,
    },
    {
        path: '',
        component: CommonLayoutComponent,
        children: [            
            {
                path: 'dashboard',
                loadChildren: './dashboard/dashboard.module#DashboardModule'
            },              
            {
                path: 'help',
                loadChildren: './help/help.modules#HelpModule'
            },     
            {
                path: '404', 
                component: Page404Component, 
                canActivate: [AuthGuardService] 
            },            
            {
                path: '', redirectTo: '/dashboard', pathMatch: 'full'
            },
            {
                path: '**', redirectTo: '/404'
            }
        ]
    }
];

In AppModule 在AppModule中

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        RouterModule.forRoot(AppRoutes, { useHash: true }),
        NgbModule.forRoot(),
        FormsModule,
        PerfectScrollbarModule,
        AngularFireModule.initializeApp(environment.firebase),
        NgxSpinnerModule,
        AngularFireDatabaseModule,
        BrowserAnimationsModule,
        ScrollToModule,
    ],    
    declarations: [
        AppComponent,
        CommonLayoutComponent,
        Page404Component,
        LoginComponent,
        Sidebar_Directives,
        DropZoneDirective,
        FileSizePipe
        ],
    providers: [AngularFireStorage, AngularFireAuth, AuthGuardService],
    bootstrap: [AppComponent]
})


export class AppModule { }

Now the AuthGuardService is used to check authentication and redirect users to login etc. 现在, AuthGuardService用于检查身份验证并将用户重定向到登录等。

export class AuthGuardService implements CanActivate { private user: Observable; 导出类AuthGuardService实现CanActivate {私有用户:Observable; private userDetails: firebase.User = null; 私人userDetails:firebase.User = null; public mUser : UsersItem; 公共mUser:UsersItem; private url : string; 私人网址:字符串;

constructor(private authAf : AngularFireAuth, private db : AngularFireDatabase, 
            private router:Router
        ) 
{
    this.user = authAf.authState;
    this.user.subscribe(
      (user) => {
        if (user) {

        }
        else {

        }
      }
    );
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
{
    const mUrl = state.url;
    console.log(mUrl);
    this.url = mUrl.substr(mUrl.lastIndexOf('/') + 1)
    if (this.userDetails != null) 
    {
      console.log("User Details are not null");
      return true;
    } 
    else
    {
         this.redirectToLogin();
         // Some logic
         this.redirectToDashboard();
         return false;                       
     }
}

} }

I want to add some separate rest apis to be accessible by some external users. 我想添加一些单独的rest api,以供某些外部用户访问。 I am not sure how can I add them. 我不确定如何添加它们。

the url should be something like 网址应该是这样的

https://localhost:42xx/api/v1/getData GET https:// localhost:42xx / api / v1 / getData GET

I have tried creating a separate components eg ApiModule however they get redirect to login page. 我尝试创建一个单独的组件,例如ApiModule,但是它们被重定向到登录页面。

Is there anyway I can make the system not trigger AuthGuardService for a specific url like above Secondly how can I create REST APi based on angular6 无论如何,我可以使系统不为上述特定URL触发AuthGuardService吗?其次,如何基于angular6创建REST APi

I don't think this is related to auth-guard. 我认为这与auth-guard无关。 The initial routing should be manged by your web server. 初始路由应由您的Web服务器管理。 If the server request has /api/* respond directly from the server. 如果服务器请求具有/api/*直接从服务器响应。 If anything else, respond with static content ie index.html , foo.js , bar.png , etc. To make it work with ng serve , you need to update your proxy.conf.json , as @penleychan mentioned. 如果还有其他问题,请使用静态内容(即index.htmlfoo.jsbar.png等)进行bar.png 。要使其与ng serve ,您需要更新您的proxy.conf.json ,如@penleychan所述。
See https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md 参见https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM