嗨,我在Typescript和Angular上是一个相对较新的人,在这种情况下,我尝试弄清楚:我在ng build上遇到了以下错误,但在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);
})
}
}
答案 0 :(得分:0)
那是因为您可能有一些声明为private
的变量,但是它们仍在特定组件的模板中使用。
解决方法是将模板中将要使用的所有那些属性设置为public
,其余属性设置为private
。这样做应该可以为您解决问题。
这里的总体思路是,如果要使用该文件之外的文件中的任何内容,则必须将其声明为public
,否则在构建时会出错。
答案 1 :(得分:0)
搜索onAuthorize()
函数的这一行:
user_id: ngRedux.getState().session,
将其更改为此:
user_id: this.ngRedux.getState().session,
您访问的是类变量而不是函数局部变量,因此您需要指定this
关键字。
所有这一切都假定您已将@angular-redux
库属性安装为项目的一部分,并且将NgRedux<IAppState>
设置为@NgModule
中的提供程序。