简体   繁体   English

如何在 angular-cli 项目中添加另一个页面?

[英]How do I add another page in an angular-cli project?

Based on the comments on another of my questions ( gradle how to add files javascript fies to a directory in the war file ) I'm trying to use angular-cli to help build and manage an angular project.基于对我的另一个问题的评论( gradle how to add files javascript fies to a directory in the war file ),我正在尝试使用 angular-cli 来帮助构建和管理 angular 项目。 However, I cannot seem to find any documentation on how to create a second webpage in the project, which to me seems like a very basic task.但是,我似乎找不到有关如何在项目中创建第二个网页的任何文档,这对我来说似乎是一项非常基本的任务。 I tried creating a "component" with ng g component {component name} , but this didn't add anything to the build result.我尝试使用ng g component {component name}创建一个“组件”,但这并没有向构建结果添加任何内容。

I had missed the section of the angular docs on routing since I did not make the connection between the word "routing" and what I wanted to do.我错过了 angular 文档中关于路由的部分,因为我没有在“路由”这个词和我想做的事情之间建立联系。 Routing as described here works perfectly when using Node as your server.使用 Node 作为服务器时,此处描述的路由非常有效。 However, other web servers such as Tomcat (which I am using for this project) will not since ng build only generates an index.html file.但是,其他 Web 服务器(例如 Tomcat(我用于此项目))不会,因为ng build仅生成一个 index.html 文件。 Node knows that it should re-route URLs under the angular base to that file, but Tomcat doesn't. Node 知道它应该将 angular base 下的 URL 重新路由到该文件,但 Tomcat 不知道。 A proxy server such as apache needs to be placed in front of the Tomcat server to redirect the urls to the base url for the application.需要在 Tomcat 服务器前面放置 apache 等代理服务器,以将 url 重定向到应用程序的基本 url。

With that out of the way, here is the basics of routing:顺便说一下,这里是路由的基础知识:

  1. create a component for each "page" (the component does not need to be responsible for the whole page displayed. see 2)为每个“页面”创建一个组件(该组件不需要负责整个页面的显示。见2)
  2. create a "shell" component that contains features that will be on all pages eg toolbar, side navigation.创建一个“外壳”组件,其中包含所有页面上的功能,例如工具栏、侧边导航。
  3. add <router-outlet></router-outlet> to the point in the shell component component where components for sub-URLs will appear (note that they are inserted into the DOM after this tag, not within it.)<router-outlet></router-outlet>到 shell 组件组件中将出现子 URL 组件的位置(注意它们是插入到此标记之后的 DOM 中,而不是在其中。)
  4. in the imports for your module, add RouterModule.forRoot() .在模块的导入中,添加RouterModule.forRoot() This function takes an array of Route .此函数采用Route数组。 Each route has a path and a component property.每个路由都有一个path和一个component属性。 path is the url (relative to the base url) that will cause component to be inserted into the DOM. path是将导致component插入 DOM 的 url(相对于基本 url)。 Note that path values should not begin with a slash.请注意, path值不应以斜杠开头。
  5. add a tags with the routerLink property bound to the url of your new page.添加arouterLink属性绑定到新页面 url 的标签。 Note that here, there should be a leading slash.请注意,这里应该有一个前导斜线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM