ERROR in src/app/product-home/product-home.component.html:1:1 - error NG8001: 'app-product-detail' is not a known element:
But If I remove the router for product, the error is gone, why? Because, I use ProductHomeComponent in app-routing.module.ts which in turn was used in file app.module.ts, But, when Angular compile template for ProductHomeComponent, it didn’t know where to find ProductDetailComponent. to solve this, import ProductHomeModule in app.module.ts like this:
1 2 3
imports: [ ProductHomeModule ]
Then the error is gone. Because, ProductHomeModule has imports ProductDetailModule, so Angular knows where to find ProductDetailComponent.
Conclusion: If you wan to use a component from another module, you need to import the module which contains the component in the module where you use the component. You may use the component by selector in templates or in the router, both need to import the module which contains the component.
How to check if page is rendered by server or client
Open the page in Chrome browser.
Right-click on the page and select View page source.
Check the body tag, if the body tag is empty, it means the page is rendered in client side, the following page is a CSR page, since the body tag only contains <app-root></app-root>. that’s for the client side rendering.
// The Express app is exported so that it can be used by serverless Functions. exportfunctionapp(): express.Express { const server = express(); const serverDistFolder = dirname(fileURLToPath(import.meta.url)); const browserDistFolder = resolve(serverDistFolder, '../browser'); const indexHtml = join(serverDistFolder, 'index.server.html'); const commonEngine = newCommonEngine();
functionrun(): void { const port = process.env['PORT'] || 4000; // Start up the Node server const server = app(); server.listen(port, () => { console.log(`Node Express server listening on http://localhost:${port}`); }); }
run();
Update angular.json file.
Add the following in architect | build | options section.
Posted onEdited onInangular Symbols count in article: 284Reading time ≈1 mins.
Introduction
The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes.
Here is an example:
1
Case
When you use getter, you would better provide setter for the property too. If you only provide getter, you might got errors, here is a real case.
We have a child component with a name property, and we want to format the name to uppercase in the child component. the name property is passed from the parent component.
When you run the code, you will get the following error:
1
TypeError: Cannot read properties of undefined (reading 'toUpperCase')
The error is caused by the formatName function in the child component, here name is undefined, we can’t call toUpperCase on undefined, but why?
1
return name.toUpperCase();
When we read name in the formatName function, it will trigger the getter of the name property, but we only provide the setter for the name property, so name is undefined.
To fix this error, we need to provide the getter for the name property.
Posted onEdited onInjavascript Symbols count in article: 365Reading time ≈1 mins.
Introduction
URL fragment is the part of the URL that comes after the # symbol.
1. A fragment in a url specify the location within the page.
How to go to the specific location in the page?
Create a empty <a> tag with id attribute set to the fragment part in url.
1 2 3 4 5 6
<aid="fragment1"> content of fragment 1 </a> <aid="fragment2"> content of fragment 2 </a>
input the following url in your browser http://example.com/page.html#fragment1, the browser will scroll to the location of the fragment1 in the page.
If you want to jump to the specific location in the page by clicking a link in the page, you can create a link with its href set to the id part of the section.
1 2
<ahref="#fragment1">Go to fragment 1</a> <ahref="#fragment2">Go to fragment 2</a>
2. Fragment never send to server in http request.
The fragment part of the URL is never sent to the server. It is only used by the browser. Take the following URL as an example, when you input it in the browser, observe the network tab in the developer tools, you will find that the fragment part is not sent to the server. the server only receives the request for the URL http://example.com/page.html.
1
https://localhost:3000/page1#fragment
3. Anything after the first # is a fragment identifier.
The url below want to set the color by the fragment part of the URL, but it won’t work because browser will parse the fragment part as #ffff&shape=circle instead of #ffff.
1
http://example.com/?color=#ffff&shape=circle
4. Changing fragment id doesn’t reload a page but create history.
When you change the fragment part of the URL, the page will not reload, but it will create a new entry in the browser history. You can use the forward and back button in the browser to navigate between the history entries.
## 5. Googlebot ignores the fragment part of the URL.
# References:
1. https://blog.httpwatch.com/2011/03/01/6-things-you-should-know-about-fragment-urls/
2. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#Fragment