typescript - 奥里利亚组件。有没有办法访问 typescript 中组件的功能

标签 typescript components aurelia

有没有办法从 View 中调用组件函数?类似的东西。

我正在寻找一种从我的 ViewPage 调用组件上的 show() 函数的方法。

组件

//component.html 
<template>
    <div id="sidebar"></div>
</template>

// component.css 
#sidebar {
    display: none;
}

// component.ts
import {bindable} from 'aurelia-framework';

export class Sidebar {

    @bindable data: Array<string>;

    show = () : void => {
        // Shows this specific side bar
    }

    hide = () : void => {
        // Hides this specific side bar
    }
}

查看

// view.html 
<template>
    <require from="./sidebar"></require>
    <sidebar data.bind="data"></sidebar>
    <button click.delegate="showSidebar()"></button>
<template>

// view.ts
export class ViewPage {

    data: Array<string> = ["Hello", "I", "Am", "Sidebar", "Content"];

    showSidebar = () : void => {
        // how to show the side bar component here?? 
        // I need something like sidebar.show();?
    }
}

最佳答案

所以与此同时,我一直在四处询问并找到了解决方案。见下文。

组件

<template>
    <div show.bind="visible" id="sidebar"></div>
</template>

export class Sidebar {

    visible: false;

    show = () : void => {
        this.visible = true;
    }

    hide = () : void => {
        this.visible = false;
    }
}

查看

<template>
    <sidebar sidebar.ref="sidebar" data.bind="data"></sidebar>
    <button click.delegate="show()"></button>
    <button click.delegate="hide()"></button>
<template>

import {Sidebar} from './sidebar';

export class ViewPage {

    sidebar: Sidebar;

    show = () : void => {
        this.sidebar.show();
    }

    hide = () : void => {
        this.sidebar.hide();
    }
}

注意sidebar.ref="sidebar"。这将组件和 View 绑定(bind)在一起。其中第一个 sidebar.ref 是组件名称,引号之间的第二个 sidebar 是 View 上的属性名称。

关于typescript - 奥里利亚组件。有没有办法访问 typescript 中组件的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38740445/

相关文章:

javascript - 使用本地模块,使用本地依赖

angular - 在 angular 2 中安装 paper.js 和 typings 文件

Angular 2 setter 和 getter

javascript - React - 新 Prop 传递给子组件后的 Axios/Fetch/Ajax 请求

electron - Aurelia, Electron : Possible EventEmitter memory leak detected

Aurelia 可通过对象属性观察

typescript - 如何在 typescript 界面中表示变量键名?

javascript - es2018 对比 es2018.promise 对比 es2018.regexp

architecture - "Reusable Business Component"是神话吗?

aurelia - 如何使用 Aurelia 创建基于 SVG 的图形