You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
I've followed the steps in the documentation to put server prerendered data into an Angular 2 application.
However, even though I can see that the data is available in the window (through the console), components can't seem to access the window like in the example i.e. window.userData. When this is attempted the application complains saying that the window is not defined.
Are we meant to access the pre-rendered data in another way?
Sounds like you're trying to access window while the code is running on the server. That won't work (the window, the DOM, etc. only exists on the client).
If you want some client-only code to access this data, wrap it in a guard clause like this, so that it doesn't throw any exception while running on the server:
if (typeof window !== 'undefined') {
// your client-only logic here
}
If you want to share some .NET-supplied data with your server-side JavaScript code, you can pass it to the boot module using the asp-prerender-data tag helper (see the docs under the heading Passing data from .NET code into JavaScript codehere)
@SteveSandersonMS Is there a way / example of making the component prerender on the server with the data passed to it from the asp-prerender-data tag helper?
At the moment I'm trying to set it from the client side using window in the components constructor. It would probably be nicer if the component rendered onto the client side with the data already in place.
Any data you pass via asp-prerender-data will be available to your boot-server.ts function as per docs. From there, it's up to you to get it to your view components and have them use it any way you like.
In Angular 2, you could use the DI system to make it available with some arbitrary key, e.g., by adding this to the providers array in boot-server.ts:
@abhilash1990 If you think you've found a problem, could you report it as a new issue? This issue was about something else and in any case is closed now :)
Hi,
I've followed the steps in the documentation to put server prerendered data into an Angular 2 application.
However, even though I can see that the data is available in the window (through the console), components can't seem to access the window like in the example i.e.
window.userData. When this is attempted the application complains saying that thewindow is not defined.Are we meant to access the pre-rendered data in another way?