我正在尝试运行混合应用程序,即角度2组件。以下是我为运行混合应用程序所做的设置,我能够运行应用程序,并且“my-app”组件也在此混合应用程序中初始化,但是当我尝试使用route(“/first”)访问组件(FirstComponent)时,它会加载FirstComponent,但会自动重定向到我的默认角度1路由。
index.html
<base href="/hybridApp/">
<my-app></my-app>
app.component.ts
@Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>' +
'<div ng-view></div>',
})
export class AppComponent {
name = 'Angular';
}
app.module.ts
@NgModule({
imports: [
BrowserModule,
UpgradeModule,
RouterModule,
FirstModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
}
}
first.module.ts
export class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy {
shouldProcessUrl(url: any) {
return url.toString().startsWith("/first");
}
extract(url: any) {
return url;
}
merge(url: any, whole: any) {
return url;
}
}
const routes : Routes = [
{
path :'first',
component: FirstComponent
}
]
@NgModule({
imports:[
RouterModule.forRoot(routes,{ useHash: true, initialNavigation: true, enableTracing: true })
],
exports:[
],
declarations:[
FirstComponent
],
providers:[
{ provide: UrlHandlingStrategy, useClass: Ng1Ng2UrlHandlingStrategy }
]
})
export class FirstModule {}
第一个组件. ts
@Component({
selector:'first-component',
templateUrl:'src/app/first.component.html',
styleUrls:['src/app/first.component.css']
})
export class FirstComponent implements OnInit{
ngOnInit(){
console.log("first component rendered..")
}
constructor(
private route:ActivatedRoute
)
{}
}
主页面
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, ['angular1App']);
});
我能从我的FirstComponent”冷杉看到日志
以下是我通过@jeffrey建议的方法看到的日志
NavigationStart {id: 8, url: "/first"}
NavigationStart {id: 8, url: "/first"}
RoutesRecognized {id: 8, url: "/first", urlAfterRedirects: "/first", state: RouterStateSnapshot}
RoutesRecognized {id: 8, url: "/first", urlAfterRedirects: "/first", state: RouterStateSnapshot}
GuardsCheckStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
GuardsCheckStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
GuardsCheckEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
GuardsCheckEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
ResolveStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
ResolveStart {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
ResolveEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
ResolveEnd {id: 8, url: "/first", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
NavigationEnd {id: 8, url: "/first", urlAfterRedirects: "/first"}
NavigationStart {id: 9, url: "/auth"}
NavigationStart {id: 9, url: "/auth"}
NavigationStart {id: 9, url: "/auth"}
GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
GuardsCheckStart {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot}
GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
GuardsCheckEnd {id: 9, url: "/auth", urlAfterRedirects: UrlTree, state: RouterStateSnapshot, shouldActivate: true}
NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}
NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}
NavigationEnd {id: 9, url: "/auth", urlAfterRedirects: "/auth"}
看到什么路由器,之前、期间和房颤
constructor(
private router: Router
) {
this.router.events.subscribe(e => {
console.log(e)
});
}
您必须稍微挖掘一下事件日志,但它可能会为您提供有价值的见解。
如果您仅在部署时遇到此问题,请尝试以下方法:
如果您使用的是apache服务器,请将其添加到。htaccess
文件:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
如果您正在使用一个不同的服务器,如NGinx,
我正在尝试使用我的ui-router form angularjs和angular 5应用程序。现在我有一些http_interceptor的问题,无论如何,首先我必须降级app-component,并插入我的index.js文件。其次,我试图知道url处理策略是否有效,angular 5是否开始管理路由。第三,我必须在angularjs模块中注入angular5 urlHandling策略和来自angular5的路由。之后,我的角5工作。经过几个小时的搜索,我找到了一个要点。
链接:
我的目标是让http://mydomain/route1呈现React组件Component1,让http://mydomain/route2呈现component2。因此,我认为编写如下路线是很自然的: http://mydomain/route1按预期工作,但http://mydomain/route2反而呈现Component1。 然后我读了这个问题,并将路线改为:
所以我有一些路线,比如<code>/category/tech</code>和<code</categority/tech/new<code>和<code>/category/tech/old</code>等等 他们都使用 或者我必须分别定义它们,就像
我已经稍微调整了React路由器的私有路由示例,以便与Redux玩得很好,但是当链接或重定向到其他页面时,不会呈现任何组件。这个例子可以在这里找到: https://reacttraining.com/react-router/web/example/auth-workflow 他们的PrivateRoute组件如下所示: 但是,因为我已经把它合并到一个Redux应用程序中,我不得不稍微调整一下私
有什么简单的方法可以实现这一点吗? 这是我导航到路线的代码 这是我的路由模块 基本上,我希望执行与CreateAlbum组件是当前组件的子组件相同的操作,在这种情况下,我将使用@Input()
我是新的反应,不知道为什么这不会起作用。 我的路由器代码在这里: 因此,这段代码所发生的情况是: localhost/显示良好
Angular2有一个routerLinkActive指令,如果路由是活动的,它会向元素添加类。我想在路由不活动时添加一个类。有办法做到这一点吗?
当我将angular2测试应用程序从beta-14转换为rc1时。我犯了以下错误 发出此错误的代码在这里 导致错误的代码是main。ts 为了向后兼容,我使用了不推荐使用的路由器。此代码用于beta-14。 我用https://plnkr.co/edit/yRKhoOelf2uJ3yAsdIMm?p=preview.config.js LocationStrategy, HashLocationS