简体   繁体   English

Angular 2 Beta路由错误:模板解析错误:无法绑定到“ router-link”,因为它不是已知的本机属性

[英]Angular 2 Beta Routing Error: Template parse errors: Can't bind to 'router-link' since it isn't a known native property

I just started learning angular 2 and I am trying to set up a router. 我刚刚开始学习角度2,并尝试设置路由器。 I checked out other blogs about this and nothing has worked so far. 我查看了其他有关此内容的博客,到目前为止没有任何成果。 I have a basic setup with an apps Component that would be shown when the user clicks the links at the top of the page. 我有一个带有apps组件的基本设置,当用户单击页面顶部的链接时将显示该组件。

Here is the code I have so far. 这是我到目前为止的代码。 I am sure that something is missing. 我确定有些东西丢失了。 Please let me know if so. 如果是这样,请告诉我。

index.html 的index.html

<html>

<head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js',
                baseURL: '/',
                transpiler: 'typescript',
            }
        }
    });
    System.import('app/main')
        .then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->

<body>
<my-app>Loading...</my-app>
</body>

</html>

app.component.ts app.component.ts

import {Component}from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router';
import {apps} from './apps.component';
// Root level app
@Component({
selector: 'my-app',
directives: [ROUTER_DIRECTIVES],
template: `
    <ul>
        <li><a href="#">Home</a></li>
        <li><a [router-link]="['/apps']">Apps</a></li>
        <li><a href="#">GPS</a></li>
        <li><a href="#">Settings</a></li>
    </ul>

    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <router-outlet></router-outlet>
            </div>
        </div>
    </div>
`
 })

@RouteConfig([{
  path: '/apps',
  as: 'apps',
  component: apps
}])
export class AppComponent {}

apps.component.ts apps.component.ts

import {Component} from 'angular2/core';
import {Router} from 'angular2/router';

@Component({
 selector: 'apps-menu',
 template: '<h1>I am the apps</h1>'
})

export class apps{

}

main.ts main.ts

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {ROUTER_PROVIDERS} from 'angular2/router';

bootstrap(AppComponent, [ROUTER_PROVIDERS]);

Do follow camelCase while using directives on view. 在视图上使用指令时,请遵循camelCase Use kebab has been deprecate in angular late alpha version only. Use kebab仅在角度后期alpha版本中不推荐使用。

It should be routerLink instead of route-link 应该是routerLink而不是route-link

<li><a [routerLink]="['/apps']">Apps</a></li>

You should inject ROUTER_DIRECTIVES during the bootstrap process. 您应该在引导过程中注入ROUTER_DIRECTIVES

You need to set up your @RouteConfig a little differently. 您需要对@RouteConfig进行一些不同的设置。

@RouteConfig([ {
     path      : '/apps',
     name      : 'apps', // notice "name" here not "as"
     component : apps
} ])

Then when you are trying to call a Route you need to do: 然后,当您尝试呼叫路线时,您需要执行以下操作:

<a [routerLink]="['apps']"> 

暂无
暂无

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

相关问题 Angular 6 日历模板解析错误:无法绑定到“视图”,因为它不是“div”的已知属性 - Angular 6 Calendar Template parse errors: Can't bind to 'view' since it isn't a known property of 'div' 错误:模板解析错误:无法绑定到“myProperty”,因为它不是“myComponent”的已知属性 - Error: Template parse errors: Can't bind to 'myProperty' since it isn't a known property of 'myComponent' 未捕获的错误:模板解析错误:无法绑定到“配置文件”,因为它不是“ app-trnmnt-name”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'profiles' since it isn't a known property of 'app-trnmnt-name' 模板解析错误:由于它不是ng-template的已知属性,因此无法绑定到ngOutletContext。 1.如果&#39;ngOutletContext&#39;是一个Angula - Template parse errors: Can't bind to 'ngOutletContext' since it isn't a known property of 'ng-template'. 1. If 'ngOutletContext' is an Angula “无法绑定到&#39;ngFor&#39;,因为它不是已知的本机属性” - “Can't bind to 'ngFor' since it isn't a known native property” 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 Angular 2无法绑定到'ngif',因为它不是已知属性 - Angular 2 Can't bind to 'ngif' since it isn't a known property Angular2 异常:无法绑定到“ngStyle”,因为它不是已知的本机属性 - Angular2 Exception: Can't bind to 'ngStyle' since it isn't a known native property Angular2 - 无法绑定到&#39;ngSwitchCase&#39;,因为它不是已知的本机属性 - Angular2 - Can't bind to 'ngSwitchCase' since it isn't a known native property Angular2 - “无法绑定到&#39;ngSwitchWhen&#39;,因为它不是&#39;模板&#39;的已知属性。” - Angular2 - “Can't bind to 'ngSwitchWhen' since it isn't a known property of 'template'.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM