更新到 angular2 beta.1 后,路由器不再工作。它在 beta.0 中做到了
after update to angular2 beta.1, the router no longer works. It did in beta.0
我的测试页使用 beta.0 进行路由,但不使用 beta.1。 Atom-typscript 显示没有错误,页面加载时控制台也没有错误,但锚选项没有 link 颜色 - 它们有文本颜色。单击锚点时,会出现此控制台错误。
ERROR CONTEXT: angular2.min.js:17:5927
13:21:22.030 Object { element: <a>, componentElement: <residence-app>, context: Object, locals: Object, injector: Object } angular2.min.js:17:5927
13:21:22.038 Error: EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: this.directive_0_0.onClick is not a function
相关代码为:
app.ts(boot.ts 文件为所有组件执行 bootstrap)
//various imports
@Component({
selector: 'residence-app',
templateUrl: "angular2-oPost/src/components/navigation/headerFooter.html",
styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
directives: [ ROUTER_DIRECTIVES, Home, PostApartment4Rent ]
@RouteConfig( [
new Route({ path: "/home", name: "Home", component: Home, useAsDefault: true }),
new Route({ path: "/postApartment4Rent ", name: "PostApartment4Rent", component: PostApartment4Rent })
] )
export class HeaderFooter { }
headerFooter.html
<header>
<!-- several divs-->
<a [routerLink]="['/Home']">Home</a>
<a [routerLink]="['/PostApartment4Rent']">Rent Apartment in hF</a>
</header>
<div class="partialPage">
<router-outlet></router-outlet>
</div>
<footer>
<!-- several divs-->
</footer>
home.ts
@Component({
selector : "home",
styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
templateUrl: "angular2-oPost/src/components/navigation/home.html",
directives: [ RouterLink ]
})
export class Home { }
postApartment4Rent.ts - 与 home.ts 相同,除了 @Component 中的选择器和 class 语句
有什么解决方法?发行说明未提及路由器的重大更改。
此错误在 angular2.beta.7 中仍然存在。它是由您可以在此处找到的相同错误引起的:https://github.com/angular/angular/issues/6380
问题是由于使用 webpack(使用 UglifyJS)缩小应用程序引起的,您可以通过在 webpack.config.js
中删除类似于以下代码的内容来临时解决此错误
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'bundle.js'),
new webpack.optimize.UglifyJsPlugin()
],
问题是您的应用无法投入生产,因为您的 bundle.js 太胖了,但您可以开发您的应用 ;)
我检查了问题是否来自 UglifyJS 配置,但没有,我确认这是 Angular2 的问题。
如果我找到解决问题的方法,我会回来帮助你,祝你好运;)
我的测试页使用 beta.0 进行路由,但不使用 beta.1。 Atom-typscript 显示没有错误,页面加载时控制台也没有错误,但锚选项没有 link 颜色 - 它们有文本颜色。单击锚点时,会出现此控制台错误。
ERROR CONTEXT: angular2.min.js:17:5927
13:21:22.030 Object { element: <a>, componentElement: <residence-app>, context: Object, locals: Object, injector: Object } angular2.min.js:17:5927
13:21:22.038 Error: EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: this.directive_0_0.onClick is not a function
相关代码为:
app.ts(boot.ts 文件为所有组件执行 bootstrap)
//various imports
@Component({
selector: 'residence-app',
templateUrl: "angular2-oPost/src/components/navigation/headerFooter.html",
styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
directives: [ ROUTER_DIRECTIVES, Home, PostApartment4Rent ]
@RouteConfig( [
new Route({ path: "/home", name: "Home", component: Home, useAsDefault: true }),
new Route({ path: "/postApartment4Rent ", name: "PostApartment4Rent", component: PostApartment4Rent })
] )
export class HeaderFooter { }
headerFooter.html
<header>
<!-- several divs-->
<a [routerLink]="['/Home']">Home</a>
<a [routerLink]="['/PostApartment4Rent']">Rent Apartment in hF</a>
</header>
<div class="partialPage">
<router-outlet></router-outlet>
</div>
<footer>
<!-- several divs-->
</footer>
home.ts
@Component({
selector : "home",
styleUrls: [ "angular2-oPost/src/commonStyles/headerFooter.css" ],
templateUrl: "angular2-oPost/src/components/navigation/home.html",
directives: [ RouterLink ]
})
export class Home { }
postApartment4Rent.ts - 与 home.ts 相同,除了 @Component 中的选择器和 class 语句
有什么解决方法?发行说明未提及路由器的重大更改。
此错误在 angular2.beta.7 中仍然存在。它是由您可以在此处找到的相同错误引起的:https://github.com/angular/angular/issues/6380
问题是由于使用 webpack(使用 UglifyJS)缩小应用程序引起的,您可以通过在 webpack.config.js
中删除类似于以下代码的内容来临时解决此错误plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'bundle.js'),
new webpack.optimize.UglifyJsPlugin()
],
问题是您的应用无法投入生产,因为您的 bundle.js 太胖了,但您可以开发您的应用 ;)
我检查了问题是否来自 UglifyJS 配置,但没有,我确认这是 Angular2 的问题。
如果我找到解决问题的方法,我会回来帮助你,祝你好运;)