簡體   English   中英

帶淘汰賽的RequireJs

[英]RequireJs with Knockout

我試圖將Knockout與require js一起使用,但無法綁定ViewModel中的數據。

的HTML

<!doctype html>
<html lang="en">
<head>
    <title>SPA</title>
    <!-- Load the script "js/main.js" as our entry point -->
    <script data-main="js/app" src="js/lib/require.js"></script>
</head>
<body>

Today's message is: <span data-bind="text: myMessage"></span>

</body>
</html>

app.js

requirejs.config({
    "baseUrl": "js/lib",
    "paths": {
      "app": "../app",
      "jquery": "jquery",
      "knockout-3.4.0":"knockout-3.4.0",
      "custom":"../custom/custom-script",
      "customKO":"../custom/custom-knockout"

    }
});
require(['knockout-3.4.0', 'customKO'], function(ko, appViewModel) {
    ko.applyBindings(new appViewModel());
});
// Load the main app module to start the app
requirejs(["app/main"]);

main.js

define(["jquery", "knockout-3.4.0", "jquery.alpha", "jquery.beta" , "custom" , "customKO"], function($ , ko) {
    //the jquery.alpha.js and jquery.beta.js plugins have been loaded.
    $(function() {
        $('body').alpha().beta();

    });

});

custon-knockout.js

//Main viewmodel class
define(['knockout-3.4.0'], function(ko) {
    return function appViewModel() {
      var viewModel = {
        myMessage: ko.observable() // Initially blank
    };
    viewModel.myMessage("Hello, world!"); // Text appears
    };
});

但我收到以下錯誤

未捕獲的ReferenceError:無法處理綁定“ text:function(){return myMessage}”消息:未定義myMessage

你調用appViewModel作為構造(使用new ),而是創建一個局部變量viewModel ,而不是將成員添加到里面, this ,像this.myMessage = ko.observable();

您無法產生可構造的類並返回屬性

define(['knockout-3.4.0'], function(ko) {
    return function appViewModel() {
        var self = this;
        self.myMessage: ko.observable() // Initially blank
        self.myMessage("Hello, world!"); // Text appears
      };
   };
});

暫無
暫無

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

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