我如何使用' console.log '与knockout结合并订阅Knockoutjs

How do I use `console.log` in conjunction with knockout and subscribe with Knockoutjs?

本文关键字:合并 结合 Knockoutjs knockout log 何使用 console      更新时间:2023-09-26

我是第一次使用Knockoutjs,我有麻烦调试,因为我无法在控制台中记录变量。我可以看到我的JS在控制台加载正确,当我输入:

Home.TwitterFeedComponent我看到一个返回的object。我如何将console.log与淘汰和订阅结合使用?

 var Home = Home || {};
var inheriting = inheriting || {};
Home.TwitterFeedComponent = function(attributes) {
  if (arguments[0] === inheriting)
    return;
  Home.OnScreenComponent.call(this, attributes);
  var component = this;  
  var recent_tweets = ko.observableArray();
  var url = 'https://twitter.com/search.json?callback=?';
  this.attributes.twitter_user_handle.subscribe(function(value) {
    var twitter_parameters = {
      include_entities: true,
      include_rts: true,
      from: value,
      q: value,
      count: '3'
    }
    result = function getTweets(){
       $.getJSON(url,twitter_parameters, 
       function(json) {
           console.log(json)
       });
     }  
     console.log(twitter_parameters);
 });
};
Home.TwitterFeedComponent.prototype = new Home.OnScreenComponent(inheriting);
Home.TwitterFeedComponent.prototype.constructor = Home.TwitterFeedComponent;

我没有看到你的代码中的问题,但如果你想记录'Observables',你必须记录如下:

console.log(observableVar());

我有点不清楚这个问题的确切范围——但是,如果像我的问题一样,这个问题是针对在HTML中使用console.log的。

这里有一小段代码可能会有所帮助:

<div class="tab-content" data-bind="with: ClientSearch.selectedClient">
...
<table class="table table-striped table-condensed table-hover">
    <thead></thead>
    <tbody>
        <!-- ko foreach: { data: _general, as: 'item' } -->
        <tr>
            <td data-bind="text: eval( 'console.log('' le item '', item)' )"></td>
        </tr>
        <!-- /ko -->
    </tbody>
</table>
...
</div>

这段代码只是将foreach中的一个项目记录到控制台。

希望这对你有帮助!