簡體   English   中英

Knockout.js組件參數未定義

[英]knockoutjs components params undefined

我正在創建一個可在通知系統中使用的Knockout js組件。 當我向頁面添加組件時,我收到一個params是未定義的消息,看起來我的params對象沒有發送到viewmodel的構造函數。

一開始我以為我的組件加載器是問題所在,所以我手動注冊了組件,如下所示:

ko.components.register('notification-info', {
    viewModel: { require: "/notifications/info" },
    template: { require: "text!/notifications/info.tmpl.html"}
});

該組件由一個名為info.js的js文件組成

define(['knockout'], function (ko) {

function InfoNotificationViewModel(params) {
    this.type = params.type;
    this.subject = params.subject;
    this.title = params.title;
    this.content = params.content;
}

return InfoNotificationViewModel();
});

和一個名為info.tmpl.html的html模板

<div class="notification-container" data-bind="css: type">
<div class="notification-icon"><span class="glyphicon"></span></div>
<div class="notification-content">
    <div class="notification-subject" data-bind="text: subject">
    </div>
    <div class="notification-message">
        <p data-bind="text: title"></p>
        <p data-bind="text: content"></p>
    </div>
</div>

我使用組件綁定將組件添加到頁面中:

<div data-bind="component: {name: 'notification-info', params: {type: 'info', subject: '',title: '', content: ''} }"></div>

稍后,它將填充通過websocket發送的數據,因此參數將變得可觀察。

有人可以告訴我我在做什么錯,我認為這與我注冊組件的方式有關。

您應該返回viewmodel的構造函數,所以不要

return InfoNotificationViewModel();

采用

return InfoNotificationViewModel; (不帶括號)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM