Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么总是重定向到主页?_Javascript_Node.js_Angular_Angular Ui Router - Fatal编程技术网

Javascript 为什么总是重定向到主页?

Javascript 为什么总是重定向到主页?,javascript,node.js,angular,angular-ui-router,Javascript,Node.js,Angular,Angular Ui Router,我总是试图获取'/'它显示静态根组件(我主页的组件), 但是当它是'/welcome'页面时,会立即重定向到'/',并同时加载静态根组件,而不是欢迎组件 最初,我想将未经授权的用户重定向到欢迎页面,但只能在JavaScript中检查登录状态。在JS获得关于登录状态的信息后,它决定使用location.replace(“/welcome”)重定向,但是。。。角度再次转到'/' “有趣”的事实:在使用ng-serve进行调试期间没有任何路由问题,但在ng-build 我不知道出了什么问题,还有app

我总是试图获取
'/'
它显示
静态根组件
(我主页的组件), 但是当它是
'/welcome'
页面时,会立即重定向到
'/'
,并同时加载
静态根组件
,而不是
欢迎组件

最初,我想将未经授权的用户重定向到欢迎页面,但只能在JavaScript中检查登录状态。在JS获得关于登录状态的信息后,它决定使用
location.replace(“/welcome”)
重定向,但是。。。角度再次转到
'/'

“有趣”的事实:在使用
ng-serve
进行调试期间没有任何路由问题,但在
ng-build

我不知道出了什么问题,还有
app.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StaticRootComponent } from './static-root/static-root.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpService } from './http.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

const appRoute: Routes = [
  { path: '', component: StaticRootComponent, pathMatch: 'full' },
  { path: 'welcome', component: WelcomeComponent }
];

@NgModule({
  declarations: [
    AppComponent,
    StaticRootComponent,
    WelcomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    RouterModule.forRoot(appRoute),
    HttpClientModule
  ],
  providers: [HttpService],
  bootstrap: [AppComponent]
})

export class AppModule { }

如果需要,我可以在代码中删除任何其他角度文件,如下所示进行更改

const appRoute: Routes = [
  { path: '', component: StaticRootComponent },
  { path: 'welcome', component: WelcomeComponent },
  { path: '**', redirectTo: '' }
];
import { Router } from '@angular/router';
constructor(
        private router: Router
) {}
在组件文件中,按如下方式注入

const appRoute: Routes = [
  { path: '', component: StaticRootComponent },
  { path: 'welcome', component: WelcomeComponent },
  { path: '**', redirectTo: '' }
];
import { Router } from '@angular/router';
constructor(
        private router: Router
) {}
如果需要导航,请使用下面的代码,而不是
位置。替换(“/welcome”)

我找到了解决办法

我的项目基于MEAN(Mongo、Express、Angular和NODEJS)。。。最后一个是问题的根源

@Shakthifuture,你说你想看完整的代码,我开始回答:

“您还想看什么?我的数据和服务器文件不影响…”

我开始思考“如果影响呢?”:整个项目的路由都是通过Angular,但所有与站点的新连接都通过NodeJS和Express,所以我忘记了404案例

问题:

很久以前,在project根文件夹的服务器脚本文件
index.js
中,我添加了关于如果找不到输入的路径该怎么办的代码:

app.use(function (req, res, next) {
    res.redirect('/');
    // IF 404 NOT FOUND
});
更重要的是:

app.get('/', function (req, res) {
    res.sendFile(`${__dirname}/angular/index.html`)
});
// send index if path is '/'
但是对于
“/welcome”
,什么都没有,这就是重定向发生的原因

解决方案:

让我们添加
'/welcome'
处理程序:

app.get('/welcome', function (req, res) {
    res.sendFile(`${__dirname}/angular/index.html`)
});
(由于SPA,再次使用index.html)


现在它可以正常工作了

您是否尝试过:
const appRoute:Routes=[{path:'/',组件:StaticRootComponent,路径匹配:'full'},{path:'/welcome',组件:WelcomeComponent}]共享完整代码。@米兰,显然我试过了,但编译器说:
路由“/welcome”的配置无效:路径不能以斜杠开头
您是否尝试先添加
欢迎
路由?@Shakthifuture,您想查看哪些文件?我用
ng new
生成了一个项目,还没有编辑其他文件。页面仍然重定向到
'/'
,对于
this.router.navigate(['/welcome'])