选择框模型与打字稿和淘汰赛(Selection box model with typescript and knockout)

编程入门 行业动态 更新时间:2024-10-28 00:29:43
选择框模型与打字稿和淘汰赛(Selection box model with typescript and knockout)

我是新来的打字稿,并希望将下面的Knockout + js转换为knockout +打字稿。 Knockout + js正在工作,但是我仍然无法使它与打字稿一起工作....

视图:

<select data-bind="options: choices, value: selectedChoice"></select>

模型:

var MyModel = { choices: ["Blue", "White", "Black", "Yellow"], selectedChoice: ko.observable("Yellow") }; MyModel.selectedChoice.subscribe(function(newValue) { alert("the new value is " + newValue); }); ko.applyBindings(MyModel);

打字稿:

import BaseVM = require("./BaseVM"); class MyModel extends BaseVM { choices = ko.observableArray(["one", "two", "three"]); //Here selectedChoice subscribe in typescript... } export = MyModel;

I am new to typescript and will like to convert the following Knockout+js to knockout+typescript. The Knockout+js is working, however I am still failing to make it work with typescript....

View:

<select data-bind="options: choices, value: selectedChoice"></select>

Model:

var MyModel = { choices: ["Blue", "White", "Black", "Yellow"], selectedChoice: ko.observable("Yellow") }; MyModel.selectedChoice.subscribe(function(newValue) { alert("the new value is " + newValue); }); ko.applyBindings(MyModel);

Typescript:

import BaseVM = require("./BaseVM"); class MyModel extends BaseVM { choices = ko.observableArray(["one", "two", "three"]); //Here selectedChoice subscribe in typescript... } export = MyModel;

最满意答案

在类内的打字稿中,您需要将您的订阅代码放入构造函数中。 然后你可以使用“this”来访问你想要订阅的财产。

class MyModel extends BaseVM { choices = ko.observableArray(["one", "two", "three"]); selectedChoice = ko.observable("Yellow"); constructor() { this.selectedChoice.subscribe(function (newValue) { alert("the new value is " + newValue); }); } }

In typescript from within the class you'll need to put your subscription code inside of a constructor function. Then you can use "this" to access the property you want to subscribe to.

class MyModel extends BaseVM { choices = ko.observableArray(["one", "two", "three"]); selectedChoice = ko.observable("Yellow"); constructor() { this.selectedChoice.subscribe(function (newValue) { alert("the new value is " + newValue); }); } }

更多推荐

MyModel,selectedChoice,choices,typescript,电脑培训,计算机培训,IT培训"/> <met

本文发布于:2023-07-28 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:淘汰赛   模型   Selection   box   typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!