【问题标题】:Aurelio fetch infinite loopAurelio 获取无限循环
【发布时间】:2016-11-09 13:02:53
【问题描述】:

我正在开发我的第一个 Aurelia 应用程序,其中包含 typescript+asp.net 核心的框架。但是,获取操作进入无限循环,我不知道为什么。 /api/Hierarchy/Tree?id=0 不断被请求。但是没有使用 id=0 并且我不知道为什么它会在激活视图时多次请求任何东西。

树.ts

import {autoinject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@autoinject
export class Tree {
    heading = 'Tree';
    Tree = [];

    constructor(private http: HttpClient) {
        http.configure(config => {
            config
                .useStandardConfiguration()
                .withBaseUrl('/');
        });
    }

    update(id: string)
    {
        if (id == null)
        {
            id = "1";        
        }

        return this.http.fetch('api/Hierarchy/Tree?id=' + id)
            .then(response => response.json())
            .then(Tree => this.Tree = Tree);
    }

    activate() {
        return this.http.fetch('api/Hierarchy/Tree?id=1')
            .then(response => response.json())
            .then(Tree => this.Tree = Tree);
    }
}

树.html

<template>
    <div class="messageHierarchy-wrapper">
        <div class="message parents">
            <h4 class="parent" click.bind="update(Tree.parent.id)" value.bind="Tree.parent.id"></h4>
        </div>
        <div class="message current">
            <h4 class="current" value.bind="Tree.current.id"></h4>
        </div>
        <div class="message children">
            <h4 class="child" repeat.for="aChild of Tree.children" click.bind="update(aChild.id)" value.bind="aChild.id">

            </h4>
        </div>

    </div>
</template>

控制器

[Route("api/[controller]")]
    public class HierarchyController : Controller
    {
        [HttpGet("{id}")]
        // GET: Hierarchy/Tree/5
        public IActionResult Tree(int id)
        {
            List<MessageViewModel> _items = new List<MessageViewModel>()
            {
                new MessageViewModel() { ID=1, CorrelationID=1, PreCorrelationID = 1 },
                new MessageViewModel() { ID=2, CorrelationID=2, PreCorrelationID = 1 },
                new MessageViewModel() { ID=3, CorrelationID=3, PreCorrelationID = 2 },
                new MessageViewModel() { ID=4, CorrelationID=4, PreCorrelationID = 2 },
                new MessageViewModel() { ID=5, CorrelationID=5, PreCorrelationID = 2 },
                new MessageViewModel() { ID=6, CorrelationID=6, PreCorrelationID = 3 },
                new MessageViewModel() { ID=7, CorrelationID=7, PreCorrelationID = 3 }
            };

            MessageHierarchyViewModel aTree = new MessageHierarchyViewModel();
            MessageViewModel aCurrentItem = _items.FirstOrDefault(x => x.CorrelationID == id);
            aTree.Current = aCurrentItem;

            if (aCurrentItem != null)
            {
                aTree.Parent = _items.FirstOrDefault(y => y.CorrelationID == aCurrentItem.PreCorrelationID);
            }
            else
            {
                aTree.Parent = _items.FirstOrDefault(y => y.CorrelationID == 1);
            }

            aTree.Children = _items.Where(x => x.PreCorrelationID == id).ToList();

            return Ok(aTree);
        }
    }

【问题讨论】:

  • 您是否逐步了解每个请求以了解值的来源?
  • 另外,你应该在&lt;h4&gt;&lt;/h4&gt; 中使用${Tree.parent.id} 而不是value.bind。读起来会好很多
  • 这是我第一个使用 asp.net core、typescript、gulp、aurelia 等的项目。除了我试图解决的无限循环之外,我还有一些其他问题:gulp serve 在几秒钟后停止秒。使用 iisexpress 运行应用程序我得到空的打字稿文件,所以我看不到我在调试什么..
  • 超级疯狂的猜测:重命名 update() - 方法到 test() 左右。如果成功,我会尝试解释:)
  • 我更接近一点,因为我更改了更新方法并将模板更改为正确的 aerelia 语法,就像你们说的那样。我现在看到的值只有 MVC 控制器还没有收到点击元素的 id,必须是路由的东西......

标签: asp.net aurelia


【解决方案1】:

像这样工作:

树.ts

import {autoinject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@autoinject
export class Tree {
    heading = 'Tree';
    mainTree = [];

    constructor(private http: HttpClient) {
        http.configure(config => {
            config
                .useStandardConfiguration()
                .withBaseUrl('/');
        });
    }

    getTree(id: string)
    {
        if (id == null)
        {
            id = "1";        
        }

        return this.http.fetch('api/Hierarchy/Tree?id=' + id)
            .then(response => response.json())
            .then(aTree => this.mainTree = aTree);
    }

    activate() {
        return this.http.fetch('api/Hierarchy/Tree?id=1')
            .then(response => response.json())
            .then(aTree => this.mainTree = aTree);
    }
}

树.html

<template>
    <div class="messageHierarchy-wrapper">
        <h2>${heading}</h2>
        <div class="message parents">
            <h4 class="parent" click.delegate="getTree(mainTree.parent.id)">${mainTree.parent.id}</h4>
        </div>
        <div class="message current">
            <h4 class="current">${mainTree.current.id}</h4>
        </div>
        <div class="message children">
            <h4 repeat.for="aChild of mainTree.children" click.delegate="getTree(aChild.id)">
                ${aChild.id}
            </h4>
        </div>
    </div>
</template>

控制器

[Route("api/[controller]")]
    public class HierarchyController : Controller
    {
        [HttpGet("{id}")]
        [EnableCors("MyPolicy")]
        // GET: Hierarchy/Tree/5
        public IActionResult Tree([FromQuery]int id)
        {
            if (id == 0)
            {
                id = 1;
            }

            List<MessageViewModel> _items = new List<MessageViewModel>()
            {
                new MessageViewModel() { ID=1, CorrelationID=1, PreCorrelationID = 1 },
                new MessageViewModel() { ID=2, CorrelationID=2, PreCorrelationID = 1 },
                new MessageViewModel() { ID=3, CorrelationID=3, PreCorrelationID = 2 },
                new MessageViewModel() { ID=4, CorrelationID=4, PreCorrelationID = 2 },
                new MessageViewModel() { ID=5, CorrelationID=5, PreCorrelationID = 2 },
                new MessageViewModel() { ID=6, CorrelationID=6, PreCorrelationID = 3 },
                new MessageViewModel() { ID=7, CorrelationID=7, PreCorrelationID = 3 }
            };

            MessageHierarchyViewModel aTree = new MessageHierarchyViewModel();
            MessageViewModel aCurrentItem = _items.FirstOrDefault(x => x.CorrelationID == id);
            aTree.Current = aCurrentItem;

            if (aCurrentItem != null)
            {
                aTree.Parent = _items.FirstOrDefault(y => y.CorrelationID == aCurrentItem.PreCorrelationID);
            }
            else
            {
                aTree.Parent = _items.FirstOrDefault(y => y.CorrelationID == 1);
            }

            aTree.Children = _items.Where(x => x.PreCorrelationID == id).ToList();

            return Ok(aTree);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2014-03-13
    • 2021-11-03
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2020-04-21
    相关资源
    最近更新 更多