草庐IT

javascript - Angular2 路由器讨厌我的 AUX 路由

coder 2024-07-20 原文

2015 年 11 月 1 日更新

Aux 路由在 Alpha 45 中并没有得到很好的支持。 Link to Github Issue

支持@evan-plaice

原始问题

我看了 Brian Ford 关于 Angular2 路由器的演讲:https://youtu.be/z1NB-HG0ZH4

然后我开始尝试使用“Aux”路由,但我的代码抛出了大量错误。

我在 mgechev's Angular Seed 上使用 Alpha 44

在我的 app.ts 组件中,它是非常标准的,对于 RouteConfig 我有:

@RouteConfig([
  { path: '/', component: HomeCmp, as: 'Home' },
  { path: '/run', component: RunCmp, as: 'Run' },
  { path: '/manage', component: ManageCmp, as: 'Manage' },
  { path: '/user', component: UserCmp, as: 'User' },
  { path: '/settings', component: LocalSettingsCmp, as: 'Settings' },
  { aux: '/login', component: LoginCmp, as: 'Login' }
])

我的 app.html:

<div id="main-content" class="full">
  <router-outlet></router-outlet>
  <router-outlet name="login"></router-outlet>
</div>

HomeCmp 中,我放置了一个“登录”链接:

<p>Router Link to activate login form: <a [router-link]="['/',['Login']]">Login</a></p>

现在我在该组件中有其他路由器链接可以正常工作,但是当我添加此链接时出现此错误:

异常(exception):

EXCEPTION: "/" is only allowed at the beginning of a link DSL. in [null]
BrowserDomAdapter.logError @ angular2.dev.js?v=0.0.0:21835
BrowserDomAdapter.logGroup @ angular2.dev.js?v=0.0.0:21846
ExceptionHandler.call @ angular2.dev.js?v=0.0.0:4431
(anonymous function) @ angular2.dev.js?v=0.0.0:19543
NgZone._onError @ angular2.dev.js?v=0.0.0:10711
errorHandling.onError @ angular2.dev.js?v=0.0.0:10630
run @ angular2.dev.js?v=0.0.0:141
(anonymous function) @ angular2.dev.js?v=0.0.0:10651
zoneBoundFn @ angular2.dev.js?v=0.0.0:111
lib$es6$promise$asap$$flush @ angular2.dev.js?v=0.0.0:1301
angular2.dev.js?v=0.0.0:21835 

错误:

Error: "/" is only allowed at the beginning of a link DSL.
    at new BaseException (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:16034:21)
    at RouteRegistry.generate (http://localhost:5555/lib/router.dev.js?v=0.0.0:1645:17)
    at ChildRouter.Router.generate (http://localhost:5555/lib/router.dev.js?v=0.0.0:2044:43)
    at RouterLink.Object.defineProperty.set [as routeParams] (http://localhost:5555/lib/router.dev.js?v=0.0.0:1323:52)
    at AbstractChangeDetector.ChangeDetector_HomeCmp_0.detectChangesInRecordsInternal (eval at <anonymous> (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20415:14), <anonymous>:118:40)
    at AbstractChangeDetector.detectChangesInRecords (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20209:14)
    at AbstractChangeDetector.runDetectChanges (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20192:12)
    at AbstractChangeDetector._detectChangesInShadowDomChildren (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20259:14)
    at AbstractChangeDetector.runDetectChanges (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20196:12)
    at AbstractChangeDetector._detectChangesInLightDomChildren (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20253:14)

上下文:

{element: a, componentElement: home, context: HomeCmp, locals: Object, injector: Injector…}

它还会抛出这个:

TypeError: Cannot read property 'child' of undefined
    at ChildRouter.Router.isRouteActive (http://localhost:5555/lib/router.dev.js?v=0.0.0:1803:77)
    at RouterLink.Object.defineProperty.get [as isRouteActive] (http://localhost:5555/lib/router.dev.js?v=0.0.0:1315:29)
    at AbstractChangeDetector.ChangeDetector_HomeCmp_0.detectChangesInRecordsInternal (eval at <anonymous> (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20415:14), <anonymous>:153:44)
    at AbstractChangeDetector.detectChangesInRecords (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20209:14)
    at AbstractChangeDetector.runDetectChanges (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20192:12)
    at AbstractChangeDetector._detectChangesInShadowDomChildren (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20259:14)
    at AbstractChangeDetector.runDetectChanges (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20196:12)
    at AbstractChangeDetector._detectChangesInLightDomChildren (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20253:14)
    at AbstractChangeDetector.runDetectChanges (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20193:12)
    at AbstractChangeDetector._detectChangesInShadowDomChildren (http://localhost:5555/lib/angular2.dev.js?v=0.0.0:20259:14)

但它是在第一个错误之后出现的,所以我不确定它是由第一个错误还是其他原因引起的。

我也尝试了 Way from the Angular2 API Docs 但它会引发相同的错误。

  • 我做错了什么吗?
  • 这个语法还不被支持吗?
  • 是否有人正确使用 Aux Routes 的例子?

注意:我尝试让路由器与 Plunker 一起工作,但它不喜欢 URL 或其他东西,否则我会提供一个实际案例

2015 年 10 月 29 日更新 第一个错误是路由器链接不允许您以任何方式使用“\”或“.\”。您必须指定路由名称。

Aux 仍然不工作...

最佳答案

routerLink匹配数组中第一个参数的路径

在您的情况下,它将匹配 /Home

<a [routerLink]="['/',['Login']]">Login</a>

改用这个:

<a [routerLink]="['/Login']">Login</a>

据我所知,该参数是一个数组,因为路由还可以包含一个或多个通配符匹配项。

注意:Login在链接中使用指向路由的别名(即 as 字段)。

来源:

更新:

密切关注。 angular2 团队正计划更改 RouterLink/<router-link>RouterViewport/<router-viewport>在未来的版本中。

来源:angular/issues#4679

更新2:

  • [路由器链接] -> [routerLink]

关于javascript - Angular2 路由器讨厌我的 AUX 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33395702/

有关javascript - Angular2 路由器讨厌我的 AUX 路由的更多相关文章

  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

随机推荐