首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从父零部件访问子零部件

从父零部件访问子零部件
EN

Stack Overflow用户
提问于 2016-03-04 14:18:13
回答 1查看 136关注 0票数 1

我有一个头组件、注册组件和登录组件。在登录组件和注册组件中都使用header组件的选择器。在标题中有一个按钮。如果用户在url.../注册中,它将显示为登录按钮。单击该按钮后,路由将更改为登录

代码语言:javascript
复制
this.router.navigate(['/Login']);

​当用户移动到登录页面时,我想将该按钮更改为“注册”。如何从登录组件和注册组件控制按钮。

EN

回答 1

Stack Overflow用户

发布于 2016-03-04 15:29:35

我已经使用@Input API完成了它。

Boot.ts

代码语言:javascript
复制
import {Component,bind} from 'angular2/core';
import {ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
import {bootstrap}  from 'angular2/platform/browser';

import{ComponentOne} from 'src/cone';
import{ComponentTwo} from 'src/ctwo';

@Component({
  selector: 'my-app',
  template: `
    <h1>Component Router</h1>
    <nav>
    <hr>
      <a [routerLink]="['ComponentOne']">Login</a><hr/>
      <a [routerLink]="['ComponentTwo']">Registartion</a>
    </nav>
    <hr>
    <router-outlet></router-outlet>
  `,
  directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
  {path:'/component-one', name: 'ComponentOne', component: ComponentOne useAsDefault:true},
  {path:'/component-two', name: 'ComponentTwo', component: ComponentTwo}
])
export class AppComponent { }

    bootstrap(AppComponent, [
      ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname)
    ]);

Login.ts或cone.ts

代码语言:javascript
复制
 import {Component,View,ViewChild} from 'angular2/core';
 import {HeaderCmp} from 'src/header';


 @Component({
    template: `

    <header text="Registration"></header>
    <hr>
    <h1>Login</h1>
    `,
    directives:[HeaderCmp] 
  })

  export class ComponentOne {

      constructor(){ 
             console.log("first component being called");
      }

  }

Registartion.ts或ctwo.ts

代码语言:javascript
复制
import {Component,View} from 'angular2/core';
 import {HeaderCmp} from 'src/header';

 @Component({
    template: `
    <header text="Login"></header>
    <h1>Registration </h1>
    `,
    directives:[HeaderCmp]
  })

  export class ComponentTwo{
    constructor(){

      console.log("Second component being called");
    }
  }

Header.ts

代码语言:javascript
复制
import {Component,View,Input} from 'angular2/core';
 import {sharedService} from 'src/sharedService';

 @Component({
   selector:'header',
    template: `
    <h4>Header </h4>
    <button>{{text}}</button>
    `
  })

  export class HeaderCmp{
     @Input() text: string='myText';

      constructor(){
               console.log("Header being called");
      }

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35789259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档