This time I will show you how Angular CLI operates routing, and what are the precautions for Angular CLI operating routing. The following is a practical case, let's take a look.
Generate routes for the application.
Create a project first:
ng new my-routing --routing
Copy after login
![](https://img.php.cn/upload/article/000/000/002/3a23cbf1630ce354f8e615e646043dfb-0.png)
You can see that two module.
Look at the routing module:
![](https://img.php.cn/upload/article/000/000/002/3a23cbf1630ce354f8e615e646043dfb-1.png)
Look at the app module:
![](https://img.php.cn/upload/article/000/000/002/3a23cbf1630ce354f8e615e646043dfb-2.png)
AppRoutingModule has been imported.
Look at app.component.html again:
![](https://img.php.cn/upload/article/000/000/002/586973e1124fe1c48d4b2537be467418-3.png)
##router-outlet has been written. Very good.
Generate two more components below:
ng g c dashboard
ng g c order
Copy after login
![](https://img.php.cn/upload/article/000/000/002/586973e1124fe1c48d4b2537be467418-4.png)
Then set the routing in app-routing.module:
Modify the html again:
![](https://img.php.cn/upload/article/000/000/002/04f8aebb1743c02c12a6b4edcf19931f-6.png)
Run the application: ng serve -o
![](https://img.php.cn/upload/article/000/000/002/04f8aebb1743c02c12a6b4edcf19931f-7.png)
![](https://img.php.cn/upload/article/000/000/002/af85e95b37069fd513fd703680eeb9a7-8.png)
Yeah. No problem.
For the situation where there are multiple modules in an application.
Generate another module, and bring the routing module ( You can first use the -d parameter to view the file to be generated):
ng g m admin --routing
Copy after login
![](https://img.php.cn/upload/article/000/000/002/af85e95b37069fd513fd703680eeb9a7-9.png)
In the admin module, create an admin component:
ng g c admin
ng g c admin/email -m admin
ng g c admin/user -m admin
Copy after login
actually The -m parameter of the two commands can be removed, because the path admin/ has been specified, so it will be declared in the admin module by default instead of the app module.
![](https://img.php.cn/upload/article/000/000/002/af85e95b37069fd513fd703680eeb9a7-10.png)
will be needed later Modify app.module:
Add the admin module.
Then modify admin.component.html and add router-outlet:
![](https://img.php.cn/upload/article/000/000/002/1dd4cac2ba7c35bc1e33cf4262215cdd-11.png)
Then modify admin-routing.module.ts:
![](https://img.php.cn/upload/article/000/000/002/1dd4cac2ba7c35bc1e33cf4262215cdd-12.png)
Run: ng serve -o
Directly enter the address: http://localhost:4200/ admin
can see:
![](https://img.php.cn/upload/article/000/000/002/16418ec0a9b38eef2601b475d5c334d9-13.png)
and enter the URL: http://localhost:4200/admin/email
and you will see To:
![](https://img.php.cn/upload/article/000/000/002/16418ec0a9b38eef2601b475d5c334d9-14.png)
So no problem.
Generate Gurad.
ng g guard xxx
Copy after login
I believe you have already read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!
Recommended reading:
jQuery Cookie switching style
##Angularjs detailed explanation of the mutual communication function of controllers
The above is the detailed content of How Angular CLI operates routing. For more information, please follow other related articles on the PHP Chinese website!