ngx-cookieconsent: 无法找到名称为'NgcInitializeEvent'和'NgcStatusChangeEvent'

4

我想在使用Angular CLI建立的Angular 4应用程序中使用ngx-cookie consent包。我没有systemjs文件。因此,除了那一部分之外,我按照文档中的每个步骤进行了操作(https://tinesoft.github.io/ngx-cookieconsent/doc/index.html)。 我收到了4条错误信息。

  1. two errors with following message

    //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)
    
    1. Cannot find name 'NgcInitializeEvent'. Cannot find name 'NgcStatusChangeEvent'.

Please help.


你完成了 @NgModuleimports: [NgcCookieConsentModule.forRoot(cookieConfig), ...], 部分了吗? - angularrocks.com
是的,我已经完成了。另外,我还有另一个模块(app-routing-module.ts),我也在那里包含了它,没有使用cookieConfig参数。 - user659730
结果发现第一个错误是由于依赖项未正确安装(运行“del -rf node_modules”命令可解决-https://github.com/tinesoft/ngx-cookieconsent/issues/2)。但我仍然有第二个问题!这导致每次打开页面时都会出现cookie。 - user659730
2个回答

8
我是ngx-cookieconsent的作者。 我已经在项目的问题跟踪器上回答了这些问题,但对于未来的谷歌搜索者,这里是解决方法:
1. 找不到“NgcInitializeEvent”和“NgcStatusChangeEvent” 你需要在代码中导入它们,如果要使用这些事件:
import { NgcInitializeEvent, NgcStatusChangeEvent } from 'ngx-cookieconsent';
  1. Cookie状态未被持久化

您需要设置配置对象的'cookie.domain'参数(即使在本地主机上),以便正确设置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'
};

environments/environment.prod.ts:(生产环境)

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 {
}

如何在Angular 5中使用Cookie Consent? - yog
2
我只是在跟进这个问题 :) 当我尝试运行我的应用程序时出现错误... 错误是:An unhandled exception occurred: Script file ../node_modules/cookieconsent/build/cookieconsent.min.js does not exist. 你有任何想法为什么会出现这个错误吗?我在节点模块中有那个文件。 - Csibi Norbert
看起来你还需要从npm/yarn安装"cookieconsent"。 - Youzef

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接