【问题标题】:Requirejs with Knockout error: Unable to process binding "component: function (){return f}" - Mismatched anonymous defined() module带有敲除错误的Requirejs:无法处理绑定“组件:函数(){return f}” - 不匹配的匿名定义()模块
【发布时间】:2015-07-07 06:16:34
【问题描述】:

我在所有浏览器中都遇到了错误。我说“无法处理绑定”组件“......”。我已经阅读了很多文章,包括 requirejs 网站。我已经检查了可能导致错误的原因,但我不知道它们是否适用于我的代码。据我所知,我没有使用 script 标签手动加载任何内容,并且每个模块都加载了 requirejs。

我使用 yoman 创建了一个淘汰项目:yo ko。之后使用yo ko:component [name of component] 添加组件。该页面在大多数情况下加载正常,但会定期给出以下错误。当我使用两个组件时,频率似乎增加了。我编辑了新组件并删除了对敲除对象的引用,但错误仍然发生,尽管不那么频繁。

具体错误如下:

Uncaught Error: Unable to process binding "component: function (){return f}"
Message: Mismatched anonymous define() module: function (a){function b(a){return h.raw?a:encodeURIComponent(a)}function c(a){return h.raw?a:decodeURIComponent(a)}function d(a){return b(h.json?JSON.stringify(a):String(a))}function e(a){0===a.indexOf('"')&&(a=a.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return a=decodeURIComponent(a.replace(g," ")),h.json?JSON.parse(a):a}catch(b){}}function f(b,c){var d=h.raw?b:e(b);return a.isFunction(c)?c(d):d}var g=/\+/g,h=a.cookie=function(e,g,i){if(void 0!==g&&!a.isFunction(g)){if(i=a.extend({},h.defaults,i),"number"==typeof i.expires){var j=i.expires,k=i.expires=new Date;k.setTime(+k+864e5*j)}return document.cookie=[b(e),"=",d(g),i.expires?"; expires="+i.expires.toUTCString():"",i.path?"; path="+i.path:"",i.domain?"; domain="+i.domain:"",i.secure?"; secure":""].join("")}for(var l=e?void 0:{},m=document.cookie?document.cookie.split("; "):[],n=0,o=m.length;o>n;n++){var p=m[n].split("="),q=c(p.shift()),r=p.join("=");if(e&&e===q){l=f(r,g);break}e||void 0===(r=f(r))||(l[q]=r)}return l};h.defaults={},a.removeCookie=function(b,c){return void 0===a.cookie(b)?!1:(a.cookie(b,"",a.extend({},c,{expires:-1})),!a.cookie(b))}}
http://requirejs.org/docs/errors.html#mismatch 

以下是文件中的一些代码(如果有帮助)

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ko-cai</title>
    <!-- build:css -->
      <link href="bower_modules/semantic/dist/semantic.css" rel="stylesheet">
      <link href="bower_modules/c3/c3.css" rel="stylesheet">
      <link href="css/styles.css" rel="stylesheet">
    <!-- endbuild -->
    <!-- build:js -->
      <script src="app/require.config.js"></script>
      <script data-main="app/startup" src="bower_modules/requirejs/require.js"></script>
    <!-- endbuild -->
  </head>
  <body>
    <div>
      <!--<side-bar></side-bar>
      <page-container></page-container>-->
      <dashboard></dashboard>
      this is a test
    </div>
  </body>
</html>

require.config.js

// require.js looks for the following global when initializing
var require = {
    baseUrl: ".",
    paths: {
        "crossroads":           "bower_modules/crossroads/dist/crossroads.min",
        "hasher":               "bower_modules/hasher/dist/js/hasher.min",
        "jquery":               "bower_modules/jquery/dist/jquery",
        "knockout":             "bower_modules/knockout/dist/knockout",
        "knockout-projections": "bower_modules/knockout-projections/dist/knockout-projections",
        "signals":              "bower_modules/js-signals/dist/signals.min",
        "text":                 "bower_modules/requirejs-text/text",
        "semantic":             "bower_modules/semantic/dist/semantic",
        "lodash":               "bower_modules/lodash/dist/lodash",
        "c3": "bower_modules/c3/c3",
        "d3": "bower_modules/d3/d3",
        "config":               "../../cai/config",
        "observations":         "../../cai/observations"
    },
    shim: {
        "semantic": { deps: ["jquery"] },
        "c3": { deps: ["d3"]},
        "config": { deps: ["knockout"]},
        "observations": { deps: ["knockout","jquery"]}
    }
};

dashboard.html

<h2>dashboard</h2>
<p data-bind='text: message'></p>

dashboard.ts

/// <amd-dependency path="text!./dashboard.html" />
import ko = require("knockout");
export var template: string = require("text!./dashboard.html");
export class viewModel {
    public message = ko.observable("Hello from the dashboard component too!");
    constructor (params: any) {
    }
    public dispose() {
        // This runs when the component is torn down. Put here any logic necessary to clean up,
        // for example cancelling setTimeouts or disposing Knockout subscriptions/computeds.
    }
}

dashboard.js

define(["require", "exports", "knockout", "text!./dashboard.html"], function(require, exports, ko) {
    exports.template = require("text!./dashboard.html");
    var viewModel = (function () {
        function viewModel(params) {
            this.message = ko.observable("Hello from the dashboard component too!");
        }
        viewModel.prototype.dispose = function () {
            // This runs when the component is torn down. Put here any logic necessary to clean up,
            // for example cancelling setTimeouts or disposing Knockout subscriptions/computeds.
        };
        return viewModel;
    })();
    exports.viewModel = viewModel;
});
//# sourceMappingURL=dashboard.js.map
  1. 我做错了什么?
  2. 如何解决这个问题,因为它使测试变得非常困难?
  3. 我可以在哪里查看哪些已知问题适用于我的代码,以便寻找修复方法。

【问题讨论】:

  • 你用的是什么版本的requirejs和jquery?
  • require.js 是 2.1.15,jQuery 是 2.1.1。淘汰版是 3.2.0

标签: javascript knockout.js requirejs


【解决方案1】:

经过更多的调查,我想出了一个解决方案。可能不是最好的,但我还没有看到另一个。问题与 require.js 有关。似乎文件中包含的内容并不重要(我什至尝试了一个带有空白函数的函数并得到了不匹配错误)。然而,我注意到,当我在错误之后执行相同的 require 语句时,它实际上是有效的。我添加了一个组件加载器来敲除以获取组件的配置。下面是对我有用的加载器。希望它对某人有用,直到找到更好的解决方案或我们找到这个问题的根源。

//register a custom loader
ko.components.loaders.unshift({
getConfig: function(name,callback){
    ko.components.defaultLoader.getConfig(name, function(c){
    if (c && c.require){ //do custom loading here. make first attempt to fetch the config
        try{
        require([c.require],function(config){
            callback(config);
        });
        }catch(e){
        //todo: check that this is mismatch error and try again. else throw exception
        require([c.require], function(config){ //make the request again
            callback(config);
        });
        }
    } else {
        callback(c);
    }
    })
}
});

【讨论】:

    【解决方案2】:

    您检查过requirejs 文档吗?更准确地说,您发布的错误链接背后的列表:

    为避免错误:

    • 确保通过 RequireJS API 加载所有调用 define() 的脚本。 不要在 HTML 中手动编写脚本标签来加载具有 define() 调用它们。
    • 如果您手动编写 HTML 脚本标签,请确保它只包含 命名模块,以及具有相同的匿名模块 名称作为该文件中的模块之一未加载。
    • 如果问题是使用加载器插件或匿名模块,但 RequireJS 优化器不用于文件捆绑,使用 RequireJS 优化器。
    • 如果问题是 var define lint 方法,请使用 /*global define */(“全局”之前没有空格)注释样式。

    http://requirejs.org/docs/errors.html#mismatch

    【讨论】:

    • 我已经检查了文档。问题是我无法确定代码的哪一部分违反了任何错误。此外,它是不一致的,我编写的应用程序部分没有做任何事情。您知道如何确定其中任何一项被违反了吗?
    猜你喜欢
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2016-03-24
    • 2014-07-10
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多