【问题标题】:Not able to bind data from local json to view polymer 2无法从本地 json 绑定数据以查看聚合物 2
【发布时间】:2017-12-28 11:31:26
【问题描述】:

我正在使用 Polymer 2 并希望从本地 json 文件中绑定数据。但是每次我尝试它都无法仅获取 json 文件。

我尝试了两种方法来实现它:

1) 使用

<iron-ajax url="data/employees.json" handle-as="json" last-response="{{items}}" auto>
</iron-ajax>

文件夹结构:

<iron-list items="{{items}}" as="item">
    <template>
        <div class="item">
            <b>[[item.nickname]]</b>
            <p>[[item.phone]]</p>
        </div>
    </template>
</iron-list> 

我还导入了 iron-ajax 和 iron-list

2) 使用$.get从start中的json中提取数据并将其放入变量中以将其绑定到视图。

<script>
    class IronListClass extends Polymer.Element {
        static get is() {return 'iron-comp'}

        ready() {
            super.ready();
            var that = this;
            // $.get('data/employees.json', function(data) {
            //     that.employees = $.parseJSON(data).results;
            //     console.log(that.employees);
            // });
            $.get('data/employees.json', function(data) {
            this.employees = $.parseJSON(data).results;
            console.log(this.employees);
            }.bind(this));
        }        
    }
    window.customElements.define(IronListClass.is, IronListClass);
</script>

用那个=这个也试过了。

【问题讨论】:

    标签: javascript data-binding polymer iron-ajax


    【解决方案1】:

    您可以按照以下示例加载 json 文件,因为它已经在我的应用程序中运行。

       <iron-ajax 
                auto 
                url$="{{url}}" 
                handle-as="json" 
                last-response="{{loadedItems}}"
               >
        </iron-ajax>
    

    ...

         static get properties() { return { 
            url :{
                type:String,
                value() {return "./data/employees.json"; }
            }
    

    ...

        static get observers() { return ['checkJsonFileLoaded(loadedItems)']}
    
        checkJsonFileLoaded(j) {
           if (j) {
               this.set('items', j);
               console.log(items); //u should see the loaded json file. If so than the problem is iron-list (to publish the result)
           }
        }
    

    【讨论】:

    • 首先,非常感谢您的帮助 仍然无法工作,您能帮我弄清楚吗 static get properties() { return { url: { type: String, value() {return "./data/employees.json"} } } } 静态getobservers() { return ['checkJsonFileLoaded(items)']} checkJsonFileLoaded(j) { if (j) { this.set('items', j);控制台.log(项目); } } 给出 404
    • 接缝你没有指定路径的 json 文件。检查路径或文件
    • 我在主要问题中提供了上面的文件夹结构图像。您能否提供替代路径或查看我是否犯了错误。同样在 VSCode 中,当我输入 ./data 时,它会自动建议employee.json。可能是什么问题?
    • JSON 格式如下:- [ { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": " quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }, { "userId": 1, "id": 2, "title": "qui est esse ", "body": "est rerum" } ]
    • 找不到文件。请附上完整的控制台消息或将其链接为图片。 Json 格式还不是主题。只需尝试在上面的 console.log 消息中查看任何结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多