【问题标题】:unable to render partial html using knockoutout component无法使用淘汰组件呈现部分 html
【发布时间】:2019-01-09 03:08:21
【问题描述】:

我已经定义了如下组件

module AgeModule {

export class My_Component {
    static componentName: string = 'mycomponent';
    static templateName: string = My_Component.componentName + '-template'; 
    myObs: KnockoutObservable<any>;

    constructor(params) {

        this.myObs = params && params.myObs;

    }
    static registerComponent = () => {
        ko.components.register(My_Component.componentName, { viewModel: My_Component, template: { element: My_Component.templateName } });
    }
}

}

然后在cshtml中我注册了像

这样的组件
  AgeModule.My_Component.registerComponent();

并像

一样加载部分视图
  @Html.Partial("~/Areas/Views/Home/Partials/MyComponent.cshtml", Model)

然后在局部视图“MyComponent.cshtml”

<script type="text/html" id="mycomponent-template">
   <div> KSJSDSSSSSSSSSSSSSSSSSSSSSSSSSSSS</div>

 </script>

我把组件称为

<mycomponent params="age: ageobs"></mycomponent>

我在控制台中没有收到任何类型的错误,但我的部分视图根本没有呈现。我也不知道如何调试。如何解决?

更新

这在我绑定组件时有效

<div data-bind="component: {name: 'mycomponent', params: {age:ageobs}}">


</div>

但是如果你直接使用组件标签就不行了。

【问题讨论】:

  • 能否请您发布您如何初始化My_Component 以及如何应用绑定? ageobs 来自哪里?另外,我认为您不需要 Model 参数中的 @Html.Partial(...)

标签: c# knockout.js


【解决方案1】:

我认为组件名称必须与“MyComponent.cshtml”中的 id 匹配,因此请尝试将 static componentName: string = 'mycomponent'; 更改为 editshipment-typeOfGoods 或将 id 更改为 mycomponent-template

【讨论】:

  • 我发错了问题。名字是正确的。请查看更新后的问题
  • 我在打字稿文件下设置了断点,但是在注册组件时它只命中一次,但当它被此代码调用时它应该再次命中
猜你喜欢
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2017-11-29
  • 1970-01-01
相关资源
最近更新 更多