Closed exequiel09 closed 6 years ago
Could you try to import it like this and tell me if it changes anything?
import { CookieService } from 'angular2-cookie/core';
@salemdar No good. Still the same.
Thanks. Then the issue requires some investigation. I will try to duplicate and solve the problem after work.
I have the same issue
Issue is also present on Angular 2.4.1.
The problem is with the @Optional in the CookieService constructor
I have copied the CookieService and created my own without the CookieOptions constructor (which I don't need in my app) and injected it instead of original CookieService. It works as a workaround.
According to this documentation on the Optional - Angular Docs "A parameter metadata that marks a dependency as optional. Injector provides null if the dependency is not found.".
Do you think this is a bug introduced in the 2.4.x? Or is it something else? Haven't dug deeper on the Angular codebase. This is just a mere speculation.
So i managed to make it to be AOT-compilable by changing this line
import { CookieService } from 'angular2-cookie/services/cookies.service';
to
import { CookieService, CookieOptions } from 'angular2-cookie/core';
and finally provide a default value to the CookieOptions
by providing a value (adding in the list of providers in the NgModule
):
{ provide: CookieOptions, useValue: {} }
@salemdar @elvirdolic kindly verify if it works on your end. Thanks.
@elvirdolic I managed it without removing the @Optional
decorator.
Maybe a mere hack, but it works as expected on my end. But I think, this should be transparent to the user. What do you think?
@exequiel09 It works for me. Thanks.
@exequiel09 Thanks for checking it. Strange that @Optional
doesn't work with AOT.
I will update the documentation, but I believe that we should somehow handle this internally and save user from the complexity.
@exequiel09 Thanks for the fix! Worked for me too
Since https://github.com/salemdar/angular2-cookie/issues/39 was reported, the solution will be to do this
import { BaseCookieOptions, CookieService, CookieOptions } from 'angular2-cookie/core';
{ provide: CookieOptions, useClass: BaseCookieOptions }
Unfortunately, this only works on JIT mode not on AOT mode.
@exequiel09 Yes that worked many thx! Since I am only in development mode, I can go without AOT compilation for now.
Any thoughts on how to make it work on AOT? I get the newOpts.merge error
This workaround solved the issue for me:
export function cookieServiceFactory() {
return new CookieService();
}
and within my Module:
providers: [
{ provide: CookieService, useFactory: cookieServiceFactory }
]
Any update on this? Also is there a reason the service is not provided as a module?
@psurrey's method worked for me as well.
If anyone is seeing: no provider for t!
errors when using the Angular CLI this is where it came from for me. The CLI runs AOT checking by default, this means that even with JIT in dev in production it was doing AOT and I couldn't for the life of me find this error. I had to tear out all of our services and finally got this one.
@DennisSmolek @psurrey that worked for me as well to fix no provider for t!
but any idea why exactly this is happening?
Any update please. All your solution doesn't work for me. Idk why but cookie doesn't work when i add options.
Need some help here too. We have the no provider for t error when building angular cli in prod. Using ng build without prod works for now but we can't stay like that.
Thx
@psurrey method works just fine.
In Your app.module.ts add this
{ provide: CookieService, useFactory: cookieServiceFactory },
to providers array,
like this:
providers: [ // expose our Services and Providers into Angular's dependency injection ENV_PROVIDERS, APP_PROVIDERS, { provide: CookieService, useFactory: cookieServiceFactory } ]
then just add this function somewhere at the bottom of app.module.ts
export function cookieServiceFactory() { return new CookieService(); }
@Spawnrad , will it help?
@dlevkov I think the issue @Spawnrad has is that he needs to provide the Options with the CookieService constructor. That is exactly the point that doesn't work - for whatever reason. And with my solution I was able to work around that. So the issue is, that as soon one wants to provide the Options via CookieService constructor it does still not work...
@psurrey I think this project should export the source TS services in addition to UMD and ES2015 modules. I believe if you actually clone the raw repo and use the TS services directly, you wont have a problem with AOT. Though, it's a theory and I have not tried it :) Wish I have the time to actually make a PR for this... anyone reading this and would like to give it a shot, please let me know if it works.
@psurrey in my case CookieOptionsArgs fitted well my needs
private setCookie(sValue, exdays, sDomain) { let key = this.sCookieName; let value = sValue; let opts: CookieOptionsArgs = { expires: new Date('2030-07-19') }; this._cookieService.put(key, value, opts); }
@psurrey's method worked for me as well.
I got the "No provider for t!" exception when upgrading the angular-cli to 1.0.0-beta.32.3.
Just adding this to the providers section in my core module "fixed" the issue.
{ provide: CookieOptions, useValue: {} }
Adding below code worked in building AOT, but CookieService.
{ provide: CookieOptions, useValue: {} }
CookieService api like remove cookies not working
@elvirdolic and @psurrey , I tried to use the
{ provide: CookieOptions, useValue: {} }
for AOT configuration. It works for building aot build.js but actual functionality is not working.
Any solution to this? This issue was raised in December, including in other issues, but still no proper solution? @sreedharbukya @elvirdolic @psurrey
I mentioned this in another issue, I made the required changes in this branch.
I added a CookieModule
that will take care of providing the CookieService
and that can be imported easily. I also updated typescript and tsconfig to prepare it for a AOT build. If you want to try you can run npm run build
and npm pack
on my branch.
The thing is, I don't know gulp very well and the project structure feels very complicated to me, so I don't have the time to make a PR right now.
Btw I guess that doing something like I proposed would solve #51, #50, #47, #43 and #40.
I will release a new version this weekend, which I'll introduce CookieModule.
@sebastianhaas thanks for your effort. I'll also do some modifications with project structure in order to simplify it
Cool, that's good news :)
The new version is ready. I'll release it tonight after updating the docs and testing it with well known seed projects (and angular-cli of course).
It'll be released under name ngx-cookie
.
That closes #38 .
I have released the new version, but I am having some issues with angular-cli
.
@exequiel09 and @sebastianhaas maybe you could check the issue?
@salemdar working on non-AOT and AOT-compiled code on @angular/cli
.
Note: angular-cli
has been renamed to @angular/cli
@exequiel09 @salemdar @elvirdolic @cacogr @nukuuk I tried the following
import { CookieService, CookieOptions } from 'ngx-cookie';
and then
{ provide: CookieOptions, useValue: {} }
But I am getting the following error saying "CookieOptions only refers to a type but is being used as value here"
Could you please tell me what is the problem?
In my imports i added forChild() to CookieModule and that seemed to fix it for me. forRoot() also works.
import { BaseCookieOptions, CookieService, CookieOptions } from 'angular2-cookie';
{ provide: CookieOptions, useClass: BaseCookieOptions }
Details: OS: macOS 10.12.2 Angular version: 2.4.0 Angular-CLI version: 1.0.0-beta.24 angular2-cookie version: 1.2.6
When running the compiled code using
ng serve --aot
or running the code built using the commandng build --prod --aot
it throws the errorEXCEPTION: Uncaught (in promise): Error: No provider for CookieOptions!
When running a non-AOT compiled code, it works.
My module definition looks like this:
Stack Trace: