简体   繁体   English

Angular CLI页面生成器路由和延迟加载

[英]Angular CLI page generator routes and lazy-loading

I have interrogations about how CLI is generating pages and mostly route generation. 我对CLI如何生成页面以及主要是路由生成有疑问。 When generating a new page with the ng CLI, the page module/html/spec/scss are created and the routing module is updated. 使用ng CLI生成新页面时,将创建页面module / html / spec / scss并更新路由模块。

1) By default the page is added in the lazy-loaded fashion in the app.routing.module: 1)默认情况下,页面以延迟加载的方式添加到app.routing.module中:

 { path: 'test', loadChildren: './test/test.module#TestPageModule' },

And also in the test.module, the route : 同样在test.module中,路由:

const routes: Routes = [
  {
    path: '',
    component: TestPage
  }
];

Is also added, with an empty path => Why ? 还添加了一个空路径=>为什么? I guess I could remove it all it wouldn't change anything would it ? 我想我可以删除所有内容,而不会更改任何内容吗?

2) I've read there : limiting-provider-scope-by-lazy-loading-modules that the instance of a service is not the same with a lazy-loaded module. 2)我在这里读过: 通过延迟加载模块限制提供商范围 ,该服务的实例与延迟加载模块不同。 Then if I want a service to be available throughout the app, event though it has the property 然后,如果我希望在整个应用程序中都可以使用某项服务,则尽管该事件具有该属性,

@Injectable({
  providedIn: 'root'
})

a module that is lazy-loaded will have a new instance ? 延迟加载的模块将具有新实例吗? Or I didn't get it. 还是我不明白。

The example is simple, I have a service that fetch a list of user items and the service store that list as a property. 该示例很简单,我有一个获取用户项列表的服务,该服务将该列表存储为属性。 When I when to access the details of an item, in a lazy loaded page, the user list is empty from the injected service, so I guess that's a new instance of it... In exemples of master/detail implementations I've seen services that fetch details when navigating to it but not reusing a list tha has already been built in an other service. 当我访问项目的详细信息时,在一个延迟加载的页面中,注入服务中的用户列表为空,因此我想这是它的新实例...在我已经看到的主/详细实现示例中导航到细节但不重用列表的服务,这些服务已经在其他服务中建立。

I'd like to know why the CLI uses that way of doing things if it has such a limitation ? 我想知道,如果CLI有这样的限制,为什么CLI会采用这种方式呢?

Thanks for your help. 谢谢你的帮助。

  1. Empty path will tell router, the component to be loaded when you hit the url(in your case 'test') of module. 空路径将告诉路由器当您点击模块的url(在您的情况下为“ test”)时要加载的组件。
  2. In order to create single instance for service across the app, you need to import the service and add it to providers list in root module(like app.module.ts) 为了为整个应用程序中的服务创建单个实例,您需要导入服务并将其添加到根模块(例如app.module.ts)的提供者列表中

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

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