简体   繁体   English

在Angular2中创建链接组件

[英]Creating Link Component in Angular2

Looking to create a wrapper component in Angular 2, similar to the way that react-bootstrap and other tools allow you to create wrapper components. 希望在Angular 2中创建包装器组件,类似于react-bootstrap和其他工具允许您创建包装器组件的方式。 Looking to be able to re-use the component so we don't have to repeat the structure every time. 希望能够重用该组件,因此我们不必每次都重复该结构。

Essentially, I want to be able to re-use a component that has this rough structure: 本质上,我希望能够重用具有这种粗略结构的组件:

'use strict';
import { ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';
import {Component, Input} from 'angular2/core';

@Component({
  selector: 'nav-item',
  directives: [ROUTER_DIRECTIVES],
  providers: [ROUTER_PROVIDERS],
  template: `
  <li>
     <a routerLink="<SPECIFIED ROUTE>">
      <ng-content></ng-content>
    </a>
  </li>
  `,
})

export default class NavLink {
  public static $injector: Array<string> = [];
  @Input() to: string;
  constructor() {}
}

And then 接着

'use strict';

import {Component} from 'angular2/core';
import NavLink from './link.component';

@Component({
  selector: 'Navbar',
  host: {'class': 'navbar'},
  directives: [NavLink],
  template: `
    <div class="row">
      <ul>
        <nav-item to="/Foo">
        </nav-item>
      </ul>
    </div>
  `,
})

export default class Nav {
  constructor() {}
}

I know routerLink expects an array specification, but I'm wondering what the angular2 way of passing data from a parent component to a child component is and then accessing that data within the child component. 我知道routerLink期望使用数组规范,但是我想知道将数据从父组件传递到子组件然后在子组件中访问该数据的angular2方法是什么。 I know the @Input decorator lets you specify a property to pass in and then you can bind to that using [] in the parent component, but I'm not sure as to the best access method within a component :) 我知道@Input装饰器可以让您指定要传入的属性,然后可以使用父组件中的[]绑定到该属性,但是我不确定组件中的最佳访问方法:)

I've tried: 我试过了:

<a routerLink="{{ to }}">

But angular2 complains about not being able to .forEach over the expected array specification (which makes sense). 但是angular2抱怨无法.forEach超出预期的数组规范(这很有意义)。

EXCEPTION: TypeError: linkParams.forEach is not a function in [{{ to }} in NavLink@2:8]

I'm just not sure of the way that passing data into a bound property works; 我只是不确定将数据传递到绑定属性的方式。 I come from more of a React background, so things are passed via props and you can inspect the type and it's pretty much just normal JavaScript object-passing. 我来自React的更多背景,所以事情都是通过props传递的,您可以检查类型,这几乎只是普通的JavaScript对象传递。

What's the best way access that bound property and/or is this an antipattern I'm trying to implement? 访问绑定属性的最佳方式是什么,并且/或者这是我要实现的反模式?

I think of Angular2 Components as more powerful versions of React components, you can do more with less. 我认为Angular2组件是React组件的更强大的版本,您可以花更少的钱做更多的事情。 I am not sure if this is better or worse, but just know you probably won't need to abstract as many things, or want to abstract as many things as you did in React. 我不确定这是好是坏,但是只是知道您可能不需要抽象那么多的东西,或者想要像在React中那样抽象那么多的东西。 This is because of more prebuilt tools, which again might give you less freedom to do certain things and make it much faster to do other things. 这是因为有更多的预构建工具,这些工具可能又使您执行某些操作的自由度降低,并使其更快地执行其他操作。

I will also say, passing values back and forth in React at least for me was more intuitive. 我还要说,至少对我而言,在React中来回传递值更加直观。

So to get on with it here is a little bit about the right syntax and how Routing works in Angular2. 因此,在这里继续介绍正确的语法以及路由在Angular2中的工作方式。 Although you probably already know this, I am just going to post it anyways in case someone else reads this. 尽管您可能已经知道了这一点,但无论如何我都会将其发布,以防其他人阅读。

You would start with a @RouteConfig decorator that takes an array of objects and makes them available to your app. 您将从一个@RouteConfig装饰器开始,该装饰器接受一组对象并将其提供给您的应用程序。 So in your case you would have something like: 因此,您的情况如下:

@RouteConfig([
    {path: '/wine', name: 'Wine', component: Wine},
    //or just so you can see it in case you haven't how to add params!
    {path: '/wine/:type', name: 'WineType', component: WineType}
])

Now to access the routerlink you would pass it: 现在要访问routerlink,您将通过它:

<a [routerLink] = "['/Wine'] </a>
<a [routerLink] = "['/WineType', {type: type.ID}]}> </a>

Now to answer your question for passing things back and forth. 现在回答您的来回传递问题。

For your first component you should put this: 对于第一个组件,应输入以下内容:

  <li>
     <a routerLink="[to]">
      <ng-content></ng-content>
    </a>
  </li>

So you'll most likely want to surround your to string in square brackets so the syntax is correct for routerLink's. 因此,您很可能希望将to字符串括在方括号中,以便语法对于routerLink而言是正确的。

And for your second component Navbar the syntax is fairly close but not exact. 对于第二个组件Navbar,语法相当接近,但不完全精确。

   <div class="row">
      <ul>
        <nav-item [to]="/Wines"></nav-item>
      </ul>
    </div>

This says pass "/Wines" into the variable [to] in the child component. 这表示将“ / Wines”传递到子组件中的变量[to]。 It must be in square brackets. 它必须放在方括号中。

And to add on if you had a list of navbar items you could do something like this: 并添加导航栏项目列表,您可以执行以下操作:

'use strict';

import {Component} from 'angular2/core';
import NavLink from './link.component';

@Component({
  selector: 'Navbar',
  host: {'class': 'navbar'},
  directives: [NavLink],
  template: `
    <div class="row">
      <ul>
        <nav-item *ngFor="#item of items [to]=item>
        </nav-item>
      </ul>
    </div>
  `,
})

export default class Nav {
  all_my_routes: Array<string> = [];
  constructor() {
    all_my_routes = ['/Wine', '/OtherWine', '/Beer']
  }
}

Which should iterate through your Nav items. 哪个应该遍历您的Nav项目。

Also a side not because I think it's a bit of a silly keyword and it still gets me about 50% of the time, is make sure you use of between your ngFor #item of items . 另外一个侧面并不是因为我认为这是一个有点愚蠢的关键字,它仍然让我的时间约50%,为确保您使用of你之间ngFor #item of items

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM