ngx-cookieconsent:找不到名称'NgcInitializeEvent'和'NgcStatusChangeEvent'

问题描述 投票:2回答:1

我想在我的angular 4应用程序(使用angular cli构建)中使用ngx-cookie同意包。我没有systemjs文件。因此,除了那部分,我按照文档中的每一步(https://tinesoft.github.io/ngx-cookieconsent/doc/index.html)。我有4条错误消息

  1. 以下消息的两个错误 //Cannot read property 'initialise' of undefined at //NgcCookieConsentService.init (cookieconsent.service.js:53) //at new NgcCookieConsentService (cookieconsent.service.js:23) //at _createClass (core.es5.js:9532) //at createProviderInstance$1 (core.es5.js:9500) //at resolveNgModuleDep (core.es5.js:948 5) //at NgModuleRef.get (core.es5.js:10577) //at resolveDep (core.es5.js:11080) //at createClass (core.es5.js:10933) //at createDirectiveInstance (core.es5.js:10764) //at createViewNodes (core.es5.js:12212) 找不到名字'NgcInitializeEvent'。找不到名字'NgcStatusChangeEvent'。

请帮忙。

angular cookies angular-cli banner
1个回答
4
投票

我是ngx-cookieconsent的作者。我已经回答了关于项目问题跟踪器的问题,但是对于未来的googlers,它们是:

  1. 找不到'NgcInitializeEvent'和'NgcStatusChangeEvent'

如果要在代码中使用这些事件,则需要导入它们:

import { NgcInitializeEvent, NgcStatusChangeEvent } from 'ngx-cookieconsent';
  1. Cookie状态未保留

您需要设置配置对象的'cookie.domain'参数(即使在localhost中),以便正确设置cookie:

import {NgcCookieConsentModule, NgcCookieConsentConfig} from 'ngx-cookieconsent';

const cookieConfig:NgcCookieConsentConfig = {
  cookie: {
    domain: 'localhost' // or 'your.domain.com' // it is mandatory to set a domain (even in localhost), for cookies to work properly
  }
};

@NgModule({
  declarations: [AppComponent, ...],
  imports: [NgcCookieConsentModule.forRoot(cookieConfig), ...],  
  bootstrap: [AppComponent]
})
export class AppModule {
}

注意:如果您正在为消费应用程序使用Angular CLI,则可以利用environments根据您的运行环境控制域:

环境/ environment.ts :(开发)

export const environment = {
  production: false,
  cookieDomain: 'localhost' // -<< must be 'localhost'
};

environment / environment.prod.ts :( prod)

export const environment = {
  production: true,
  cookieDomain: 'your.domain.com' // -<< must be the domain of deployed app
};

并使用它:

import {NgcCookieConsentModule, NgcCookieConsentConfig} from 'ngx-cookieconsent';
import { environment } from './../environments/environment';

const cookieConfig:NgcCookieConsentConfig = {
  cookie: {
    domain: environment.cookieDomain // -<< domain will change base on your running env
  }
};

@NgModule({
  declarations: [AppComponent, ...],
  imports: [NgcCookieConsentModule.forRoot(cookieConfig), ...],  
  bootstrap: [AppComponent]
})
export class AppModule {
}
© www.soinside.com 2019 - 2024. All rights reserved.