【问题标题】:Directives property in angular 2角度 2 中的指令属性
【发布时间】:2017-09-27 18:25:33
【问题描述】:

我尝试使用指令 ([]) 声明我的组件,但没有成功。然后我知道在 RC6 中它已被弃用。所以我在声明@ngmodule 中添加了组件,但它仍然不知道为什么? 编辑 :: 那是我的 module.ts ::

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import {NavBarComponent} from "./Components/navbar/navbar.component";
import {AboutComponent} from "./Components/about/about.component";

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ,NavBarComponent , AboutComponent],
  bootstrap:    [ AppComponent ]
})

export class AppModule { }

那是我的 app.component.ts ::

import {  Component } from '@angular/core';
//import {AboutComponent} from "./Components/about/about.component";

@Component({
  selector: 'my-app',
  template: `<h1>{{name}}</h1>`,


})
export class AppComponent  {  }

现在 NavBar 组件有 .html 文件 ::

<nav class="navbar navbar-inverse ">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Spotify</a>
    </div>
    <div id="navbar" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>

      </ul>
    </div>
  </div>
</nav>

还有 Navbar.component.ts ::

import {Component} from "@angular/core";


@Component({
  moduleId: module.id,
  selector: 'navbar',
  templateUrl : 'navbar.component.html',
})
export class NavBarComponent  {  }

我正在使用角度快速启动,没有输出错误,但是当我重新加载页面时没有发生任何变化并且导航栏设计不显示

【问题讨论】:

  • 你使用哪个 Angular 版本?
  • 我正在使用 angular/cli 版本 1.0.1 @DmitrijKuba
  • 您能否提供一个minimal reproducible example 来解释“它没有成功” 的含义?
  • @jonrsharpe 我编辑了它,抱歉不清楚

标签: angular angular2-directives


【解决方案1】:

您似乎错过了真正应该加载组件的位置,因此您需要在某处添加&lt;router-outlet&gt;,在您的情况下为app.component。如果您希望始终显示导航栏,请将该标签放在app.component

所以你的 app.component 应该是这样的:

import {  Component } from '@angular/core';
import {NavBarComponent} from "./Components/navbar/navbar.component";

@Component({
  selector: 'my-app',
  template: `
    // Navbar always shown 
    <navbar></navbar> 

    // Your other routed components will load here 
    <router-outlet></router-outlet>
  `,
})
export class AppComponent  {  }

请查看official docs for routing 以获得更详细的说明!

【讨论】:

  • 这个答案对您有帮助吗,还是您需要进一步的帮助? :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2023-03-09
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多