首页 > 解决方案 > 从 fetch 回调中调用的 Polymer notifyPath

问题描述

我的应用程序有财产

  myData: {
    type: Array,
  },

初始化为:

[{'company_name': 'Company 1'}, {'company_name': 'Company 2'}]

从后端加载数据后,我需要在模板处触发 dom-repeat 更新数组。

<template is="dom-repeat" items="{{myData.countries}}" as="country">
    <div>[[country.name]]</div>
</template>

我遇到的问题是 notifyPath 不适用于 fetch 方法,即:

var self = this;

fetch('/api/all')
   .then(res => res.json())
   .then(json => self.myData[1]['countries'] = json)
   .then(function() {
       // not work
       self.notifyPath("myData.1.countries", self.myData[1]['countries']);
});

并且在 fetch 方法之外可以正常工作:

self.myData[1]['countries'] = [{'country_name': 'Albania'}, {'country_name': 'Mongolia'}];
self.notifyPath("myData.1.countries", self.myData[1]['countries']);

我在某个地方弄错了,还是应该以不同的方式做?

标签: javascriptpolymerweb-component

解决方案


推荐阅读