Angular2 - 导出自定义管道的方法

时间:2016-04-06 11:18:42

标签: typescript angular

我有一个导航组件,我想调用一个应用自定义管道的方法。当我尝试引用它时,我在控制台中没有定义filterPortfolio。

我将此方法绑定到我的DOM中的click事件(html由导航组件应用。

我的HTML:

<div id="filter1" class="miniNavButton" *ngIf="portfolio" (click)="changeFilter('demo')">
    <a>
        <svg class="icon icon-eye">
            <use xlink:href="symbol-defs.svg#icon-eye"></use>
        </svg>
    </a>
</div>

portfolio.component.ts:

import { Component } from 'angular2/core';
import {ViewEncapsulation} from 'angular2/core';
import {Pipe, PipeTransform} from 'angular2/core';

@Pipe({ name: 'pagesFilter' })
class pagesFilter implements PipeTransform {
    transform(pages, [key]): string {
        return pages.filter(page => {
            return page.hasOwnProperty(key);
        });
    }
}

@Component({
    selector: 'portfolio',
    templateUrl: '/app/views/portfolio.html',
    styleUrls: ['../app/styles/PortfolioMobile.css', '../app/styles/PortfolioOther.css'],
    pipes: [pagesFilter],
    encapsulation: ViewEncapsulation.None
})

export class PortfolioComponent {
    pages = [{
        img: './app/images/placeholder.png',
        name: 'veryNiceWords',
        repo: 'https://github.com/Shooshte/veryNiceWords',
        description: 'A hobby app, made to enable posting, rating and sharing quotes over social networks. Work in progress.',
        github: true
    },
        {
            img: './app/images/placeholder.png',
            name: 'ZIC IJS',
            repo: 'https://github.com/Shooshte/ZIC',
            description: 'Refurbishing of on old library webpage with AngularJS.',
            github: true
        },
        {
            img: './app/images/weather.png',
            name: 'Show the Local weather',
            repo: 'http://codepen.io/shooshte/pen/NxOwOX',
            description: 'A freeCodeCamp exercise, designed to show the local weather.',
            demo: true,
            finished: true
        },
        {
            img: './app/images/calculator.png',
            name: 'Calculator',
            repo: 'http://codepen.io/shooshte/pen/qbjJdy',
            description: 'A freeCodeCamp exercise, which requires you to build a javascript calculator.',
            demo: true,
            finished: true
        },
        {
            img: './app/images/github.png',
            name: 'MTGO Draft Replayer',
            repo: 'https://github.com/Shooshte/MTGO-Draft-Replayer',
            description: 'A simple web app that opens a MTGO draft log file, and re-creates the draft from it.',
            github: true
        },
        {
            img: './app/images/codeeval.png',
            name: 'codeEval',
            repo: 'https://github.com/Shooshte/CodeEval',
            description: 'CodeEval challenges solutions written in javascript and posted to gitHub.',
            github: true,
            finished: true
        }];

        filterPortfolio(parameter:String) {
            return this.pages ? 'pagesFilter' : parameter
        };
}

navigation.component.ts

import { Component } from 'angular2/core';
import {ViewEncapsulation} from 'angular2/core';
import { Router, ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig } from 'angular2/router';

import { LandingComponent } from './landing.component';
import { PortfolioComponent } from './portfolio.component';
import { SkillsComponent } from './skills.component';
import { ContactComponent } from './contact.component';

@Component({
    selector: 'my-navigation',
    templateUrl: '/app/views/navigation.html',
    styleUrls: ['../app/styles/navigationMobile.css', '../app/styles/navigationOther.css'],
    encapsulation: ViewEncapsulation.None,
    directives: [ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

export class NavigationComponent {
    landing = true;
    portfolio = false;

    changeMiniNavLanding = function() {
       this.landing = true;
       this.portfolio = false;
    }

    changeMiniNavPortfolio = function() {
       this.landing = false;
       this.portfolio = true;
    }

    changeFilter(a) {
        PortfolioComponent.apply(filterPortfolio(a));
    }
}

这是通过pages对象(我要将管道应用到的页面)迭代的html:

<div class="portfolioContainer">
    <div *ngFor="#p of pages" class="portfolioPageContainer">
        <img [attr.src]="p.img" class="portfolioThumbnail">
        <h2>{{ p.name }}</h2>
        <a [attr.href]="p.repo">
            <div>
                <p>{{ p.description }}</p>
            </div>
            <p class="portfolioRepoLink">See the Code!</p>
        </a>
    </div>
</div>

因此,组合组件应该导出方法以调用导航组件应该调用的过滤器。我认为这已经通过导入整个组件来完成,但我无法让它工作。

我明白这可能有点令人困惑。如果它有帮助,这里有一个github整个事件的回购 - 只需克隆,转到投资组合并运行npm start。相关文件位于视图(portfolio.html,navigation.html)和app文件夹(portfolio.component.ts,navigation.component.ts)下。

有人可以指出我做错了什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

filterPortfolio方法是PortfolioComponent类的一部分,因此您可以通过这种方式调用它。我认为您没有以正确的方式使用apply方法:

PortfolioComponent.prototype.filterPortfolio.apply(this, [a]);

call一个:

PortfolioComponent.prototype.filterPortfolio.call(this, a);

话虽这么说,我不确定您是否使用了正确的方法,但我看不到您在代码中使用pagesFilter管道的任何地方。

Günter过去对这个问题给出了很好的答案。有关详细信息,请参阅此问题: