我在 Angular 2 中创建了一个单页抵押贷款计算器应用程序,它对我来说就像一个学习 Playground (试图更习惯当前工作中使用的技术堆栈)...它在 http://www.mortgagecalculator123.com 运行如果你想看看它。如果您想查看它,我已经在页面上使用 Fork Me 链接将其开源。
无论如何,我想做的是能够直接从 URL 将变量传递给我的应用程序,以便我的 Angular 2 应用程序可以使用它们。像这样的东西:http://www.mortgagecalculator123.com/?var1=ABC&var2=DEF
我尝试了以下操作,在我的 app.component.ts 中,我添加了以下内容:
import { Router, ActivatedRoute, Params } from '@angular/router';
AppComponent {
private var1: string;
private var2: string;
constructor(
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.route.params.forEach((params: Params) => {
this.var1 = params['var1'];
this.var2 = params['var2'];
});
console.log(this.var1, this.var2);
}
...
}
但这不起作用,当我运行 npm start 时,出现以下错误:
aot/app/app.component.ngfactory.ts(45,30):错误 TS2346:提供的参数与调用目标的任何签名都不匹配。
谢谢你,任何帮助将不胜感激。
最佳答案
我创建了一个使用查询参数工作的拉取请求。我会尽力解释我所做的一切。
前面的答案不起作用的原因是因为您根本没有使用路由器。您创建了一个没有路由的大型应用程序组件。为了解决我们需要开始使用路由模块的问题,我还建议您阅读以下两个教程:Routing和 Routing & Navigation .
首先我们需要更改您的index.html
,将此添加到您的<head>
:
<base href="/">
见 here为什么添加它很重要。
那么既然您使用的是 AppComponent
显示创建新组件所需的一切,我们将其称为 RootComponent
.在您的 index.html
更改<my-app>
至<root>
;它看起来像这样:
<root>Loading...</root>
现在在您的app
中文件夹,我们需要创建两个文件,第一个是 root.component.ts
看起来像这样:
import { Component } from '@angular/core';
@Component({
selector: 'root',
template: `<router-outlet></router-outlet>`,
})
export class RootComponent {
constructor() { }
}
看看我们有 <router-outlet></router-outlet>
作为模板,Angular 会根据路由注入(inject)我们的组件。
我们还需要再创建一个文件,即 main.route.ts
,这就是它的样子:
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
export const mainRoutes: Routes = [
{ path: '', component: AppComponent }
];
export const mainRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(mainRoutes);
在这个文件中,我们说对于我们的基本路由,我们要渲染我们的 AppComponent
我们已经创建了新文件,现在我们需要在您的app.module.ts
中告诉我们的应用模块。所以我们导入新文件并声明新组件。我们还需要更改我们的 boostrap 组件:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {AppComponent} from './app.component';
import {RootComponent} from './root.component'; // we import our new RootComponent
import {ChartModule} from 'primeng/primeng';
import {TooltipModule} from 'primeng/primeng';
import { routing, mainRoutingProviders } from './main.routes'; // We also import our Routes
@NgModule({
imports: [
BrowserModule,
ChartModule,
FormsModule,
mainRoutingProviders, // we also need to import our route provider into the module
ReactiveFormsModule,
routing, // and also import our routes declarations
TooltipModule
],
declarations: [AppComponent, RootComponent], // we declare our new RootCpmponent
bootstrap: [RootComponent] // Notice that we are now using our RootComponent to bootstrap our app
})
export class AppModule {
}
现在一切就绪,我们现在终于可以开始在您的 AppComponent
上向我们的应用程序传递参数了。导入Router
, ActivatedRoute
和 Params
来自 @angular/router
所以你的AppComponent
看起来像这样:
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
export class AppComponent implements OnInit, OnDestroy {
private var1: string;
private var2: string;
private sub: Subscription;
constructor(
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
// assign the subscription to a variable so we can unsubscribe to prevent memory leaks
this.sub = this.route.queryParams.subscribe((params: Params) => {
this.var1 = params['var1'];
this.var2 = params['var2'];
console.log(this.var1, this.var2);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
...
}
你可以看到拉取请求 here
关于javascript - Angular 2 - 如何传递 URL 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858471/