TS2304:找不到名称



嗨,有人说我对Typescript和Angular还比较陌生,在这种情况下我试着弄清楚:我在ng构建时出现了下面的错误,但在ng中,一切都很顺利。。奇怪的是这个问题。我也想做一些私人声明,但私人声明不起作用。可能需要了解更多的技巧。。。

TS2304:找不到名称"ngRedux"。

import { Component, OnInit, AfterViewChecked, OnDestroy, ViewEncapsulation} from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { MatDialog } from '@angular/material';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { PayPalConfig, PayPalEnvironment, PayPalIntegrationType } from 'ngx-paypal';
import { MatRadioChange } from '@angular/material';
import { CookieService } from 'ngx-cookie-service';
import { UsersProvider} from '../../../../../providers/users.provider';
import { NgRedux } from '@angular-redux/store';
import {IAppState} from '../../../../../reducers';

declare let paypal: any;
@Component({
selector     : 'abbonamento',
templateUrl  : './abbonamento.component.html',
styleUrls    : ['./abbonamento.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class AbbonamentoComponent implements OnInit, OnDestroy, AfterViewChecked
{
addScript: boolean = false;
paypalLoad: boolean = true;
public finalAmount: number = 1;
abbonamenti = [];
caratteristiche = [];
public PayPalConfig?: PayPalConfig;
constructor(
public http: Http,
public cookieService: CookieService,
public ngRedux: NgRedux<IAppState>,
)
{
}
ngOnInit()
{
this.http.get('https://dev.site.it/get-abbonamenti').map(res => res.json()).subscribe(data => {
this.abbonamenti = data.abbonamenti;
this.caratteristiche = data.caratteristiche;
});
}
ngOnDestroy()
{
}

payPalConfig = {
env: 'sandbox',
client: {
sandbox: 'xxxxxxxxxxxxxxxxxxx',
production: 'xxxxxxxxxxxxxxxxxxxxx'
},
commit: true,
// payment() is called when the button is clicked
payment: function (data, actions) {
// Make a call to the REST api to create the payment
let prezzo = cookieService.get('importo');
return actions.payment.create({
payment: {
transactions: [
{
amount: {total: prezzo, currency: 'EUR'}
}
]
}
})
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function (data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then((response) => {
console.log(response);
var data = {
user_id: ngRedux.getState().session,
dati_transazione: response,
}
http.post('https://dev.sito.it/aggiorna-abbonamento', data).subscribe(data=> {});
window.alert('Pagamento confermato');
});
}
}
pianoChange(event: MatRadioChange)
{
this.cookieService.set( 'importo', event.value );
}

ngAfterViewChecked(): void {
if (!this.addScript) {
this.addPaypalScript().then(() => {
paypal.Button.render(this.payPalConfig, '#paypal-checkout-btn');
this.paypalLoad = false;
})
}
}
addPaypalScript() {
this.addScript = true;
return new Promise((resolve, reject) => {
let scripttagElement = document.createElement('script');
scripttagElement.src = 'https://www.paypalobjects.com/api/checkout.js';
scripttagElement.onload = resolve;
document.body.appendChild(scripttagElement);
})
}
}

搜索onAuthorize()函数的这一行:

user_id: ngRedux.getState().session,

更改为:

user_id: this.ngRedux.getState().session,

您访问的是类变量,而不是函数局部变量,因此需要指定this关键字。

所有这些都假设您已将@angular-redux库属性作为项目的一部分安装,并且NgRedux<IAppState>已设置为@NgModule中的提供程序。

这是因为您可能有一些变量声明为private,但它们仍在特定组件的模板中使用。

解决此问题的方法是将您将在模板中使用的所有属性设置为public,其余属性和private。这样做应该可以为您解决问题。

这里的总体思想是,如果您想使用该文件之外的文件中的任何内容,则必须将其声明为public,否则在构建时会出现错误。

最新更新