KnockoutJS运行报错Uncaught TypeError: this.firstname is not a function求助
报错原因
核心错误为属性名大小写不匹配:
- 定义可观察属性时使用的命名是
firstName、lastName,其中Name的首字母为大写 - 在
ko.computed计算逻辑中调用时写为firstname、lastname,name的首字母为小写,和定义的属性名不一致,访问了不存在的属性,因此触发「不是函数」的类型错误。
修正后的代码
<span data-bind="text: fullname"></span> <script type="text/javascript"> function AppViewModel() { this.firstName = ko.observable('Bob'); this.lastName = ko.observable('Smith'); this.fullname = ko.computed(function() { // 修正为和定义一致的大小写写法 return this.firstName() + " " + this.lastName(); },this); } var vm = new AppViewModel(); ko.applyBindings(vm); </script>
内容的提问来源于stack exchange,提问作者原裕貴
相关产品推荐
相关产品推荐

