草庐IT

javascript - Angular 路由 : Instance Creation vs Instance Activation

coder 2025-02-21 原文

Angular Routing 文档提到了组件实例创建、组件实例激活和路由激活。

文档没有解释这些概念的区别,以及每次创建/激活发生的时间。


问题

  1. 实例创建和实例激活有什么区别?
  2. 实例激活和路由激活有什么区别?
  3. 实例激活是否总是与实例创建同时发生?

总结:不清楚组件实例激活和路由激活的真正含义,以及它们与组件实例的关系创作(尤其是时间安排)。


已知信息

实例创建

  • 组件实例由 Angular 在不同类型的组件之间导航时创建
  • 在同一组件的实例之间导航时,默认会重复使用这些实例

实例激活

  • 当浏览器的位置 URL 更改以匹配路径段(例如/crisis-center)时,路由器会激活相应组件(例如 CrisisListComponent)的实例并显示其 View
  • 当应用程序请求导航到路径(例如/crisis-center)时,路由器会激活相应组件(例如 CrisisListComponent)的实例,显示其 View ,并使用该路径的 URL 更新浏览器的地址位置和历史记录

路由激活

  • 在整个文档中提到了几个地方。见下文

Angular 文档引用

以下是 Angular 文档中对上述三个概念的一些提及:

实例创建

By default, the router re-uses a component instance when it re-navigates to the same component type without visiting a different component first.

...

This application won't re-use the HeroDetailComponent. The user always returns to the hero list to select another hero to view. There's no way to navigate from one hero detail to another hero detail without visiting the list component in between. Therefore, the router creates a new HeroDetailComponent instance every time.

Link

实例激活

When the browser's location URL changes to match the path segment /crisis-center, then the router activates an instance of the CrisisListComponent and displays its view.

Link

When the application requests navigation to the path /crisis-center, the router activates an instance of CrisisListComponent, displays its view, and updates the browser's address location and history with the URL for that path.

Link

路由激活

The data property in the third route is a place to store arbitrary data associated with this specific route. The data property is accessible within each activated route.

Link

You can also protect child routes with the CanActivateChild guard. The CanActivateChild guard is similar to the CanActivate guard. The key difference is that it runs before any child route is activated.

Link

In the Hero Detail and Crisis Detail, the app waited until the route was activated to fetch the respective hero or crisis.

Link

The ActivatedRouteSnapshot contains the future route that will be activated and the RouterStateSnapshot contains the future RouterState of the application, should you pass through the guard check.

Link

最佳答案

What is the difference between instance creation and instance activation?

实例化意味着创建路由(ActivateRoute)或组件的实例。激活路由意味着将其附加到 router-outlet 指令。激活组件意味着将其附加到 DOM。使用 activateWith 激活路由和组件router-outlet 指令的功能。

让我们看一些例子。假设您有以下路线:

{
    path: 'a',
    component: AComponent,
    children: [
        { path: 'b', component: BComponent },
        { path: ':name', component: DComponent }
    ]
}

现在您导航到 a/b

路由器将:

  • 实例化{ path: 'a', component: AComponent, children: [] } route
  • 实例化{ path: 'b', component: BComponent } 路由
  • 通过将它们附加到相应的router-outlet 位置来激活这些路由
  • 使用this approach 实例化AComponentBComponent
  • 通过将 AComponentBComponent 添加到 DOM 来激活它们

现在您导航到 a/n1

路由器将:

  • a 重用路由 - { path: 'a', component: AComponent, children: [] 路由(无实例化或激活)
  • 实例化{ path: ':name', component: DComponent } 路由
  • 激活{ path: ':name', component: DComponent } 路由
  • 重用AComponent实例(没有实例化或激活)
  • 实例化DComponent实例
  • 通过将 DComponent 附加到 AComponent View 中的 router-outlet 来激活它

现在您导航到 a/n2

路由器将:

  • a 重用路由 - { path: 'a', component: AComponent, children: [] 路由(无实例化或激活)
  • n2 重用路由 - { path: ':name', component: DComponent } 路由(没有实例化或激活)
  • 更新n2激活路由的参数
  • 重用 DComponent 实例(没有实例化或激活)

关于javascript - Angular 路由 : Instance Creation vs Instance Activation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155804/

有关javascript - Angular 路由 : Instance Creation vs Instance Activation的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  3. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  4. ruby-on-rails - Rails - 从命名路由中提取 HTTP 动词 - 2

    Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba

  5. ruby-on-rails - 如何在 Rails 中设置路由的默认格式? - 2

    路由有如下代码:resources:orders,only:[:create],defaults:{format:'json'}resources:users,only:[:create,:update],defaults:{format:'json'}resources:delivery_types,only:[:index],defaults:{format:'json'}resources:time_corrections,only:[:index],defaults:{format:'json'}是否可以使用1个字符串为所有资源设置默认格式,每行不带“默认值”散列?谢谢。

  6. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  7. ruby - cucumber 的路由问题 - 2

    我正在使用rails3和cucumber,除了这个小问题,一切都很顺利GivenIamonthe"editautomobile"pageNoroutematches{:controller=>"automobiles",:action=>"edit"}(ActionController::RoutingError)现在路径在paths.rb中设置为edit_automobile_path在routes.rb中我有汽车作为资源,我搭建了它所以请告诉我我遗漏了什么,清楚地定义了路线并且匹配,因为我运行了rake路线并看到了路线。请指出正确的方向 最佳答案

  8. ruby - Rails 路由 : Giving default values for path helpers - 2

    有什么方法可以为url/path助手提供默认值吗?我有一个可选范围环绕我的所有路线:#config/routes.rbFoo::Application.routes.drawdoscope"(:current_brand)",:constraints=>{:current_brand=>/(foo)|(bar)/}do#...allotherroutesgohereendend我希望用户能够使用这些URL访问网站:/foo/some-place/bar/some-place/some-place为了方便起见,我在我的ApplicationController中设置了一个@current

  9. ruby-on-rails - 将 Rails 路由助手作为类方法添加到类中 - 2

    我如何将像“root_path”这样的Rails路由助手作为类方法添加到像my_model.rb这样的类中?所以我的课是这样的:ClassMyModeldefself.fooreturnself.root_pathendendMyModel.foo以上不起作用,因为ClassMyModel不响应root_path这是我所知道的:我可以使用includeRails.application.routes.url_helpers,但这只会将模块的方法添加为实例方法我试过扩展Rails.application.routes.url_helpers但它没用请随时给我上课:)

  10. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

随机推荐