简体   繁体   English

设置URL而不刷新Aurelia中的整个模板

[英]Set URL without refreshing whole template in Aurelia

In my Aurelia webapp, I have a page with links. 在我的Aurelia网络应用中,我有一个带有链接的页面。 These are caught with a click.delegate="" , which goes to a method in the code-behind. 这些是通过click.delegate=""捕获的,该代码转到后面代码中的方法。 The view code will then use http-fetch to make AJAX calls and then refresh the data just by updating an internal variable which is bound to some elements in the HTML template. 然后,查看代码将使用http-fetch进行AJAX调用,然后仅通过更新绑定到HTML模板中某些元素的内部变量来刷新数据。 Works perfectly, except for one thing - it's not reflected in the URL location bar (in the browser -- so bookmarking is impossible). 完美运行,除了一件事-它没有反映在URL位置栏中(在浏览器中-因此无法添加书签)。

One solution is to create a route with a parameter. 一种解决方案是使用参数创建路由。 This will call the activated() method, w. 这将调用activated()方法w。 the param. 参数。 I've observed that the constructor is not called over and over (when navigating to the same route, but with different params), so I guess the same instance of the ViewModel is being used. 我观察到,构造函数并没有一遍又一遍地调用(导航到相同的路线,但是具有不同的参数时),所以我猜想正在使用相同的ViewModel实例。

{ route: 'cell-detail/:id', name: 'cell-detail', moduleId: 'cell-detail',     title: 'cell-detail' },

However, I want to know (and prevent) the whole HTML template from being rendered every time, not just the part of the changed bound variable. 但是,我想知道(并防止)每次都呈现整个HTML模板,而不仅仅是更改的绑定变量的一部分。 I'm already using activationStrategy.invokeLifecycle , so maybe it's already working this way. 我已经在使用activationStrategy.invokeLifecycle ,所以也许它已经以这种方式工作了。 I don't know how to find out. 我不知道如何找出答案。

Is this correct, and if so, is there any way to update the URL/location bar, without repainting the whole HTML template (set a new location to reflect internal changes w/o using the router maybe)? 这是正确的吗?如果是这样,是否可以在不重新绘制整个HTML模板的情况下更新URL /位置栏(设置新位置以反映不使用路由器的内部更改)?

Update Observing the DOM changes in the Elements tab in Chrome Developer Tools, it seems like Aurelia only updates what's changed - like React's shadow-dom. 更新观察Chrome开发者工具中“ 元素”标签中的DOM变化,看来Aurelia仅更新了更改内容-如React的shadow-dom。 At least there's a big visible difference (the HTML lights up/flashes in the dev console) if using activationStrategy.replace . 如果使用activationStrategy.replace至少会有很大的可见差异(HTML在开发控制台中点亮/闪烁)。

However, there seems to be a small difference when using the router vs. just updating an internal bound variable, so if anyone knows for sure what's happeing, that would be enlightening. 但是,使用路由器与仅更新内部绑定变量之间似乎有很小的差异,因此,如果有人确定是什么原因,那将是一个启发。

Why don't you just use the History's API pushState method 您为什么不只使用History的API pushState方法

 history.pushState({someState : someStateValue}, "new title", newUrl);

Of course, you'll need to handle the restoring of the proper state yourself when the user deeplinks to that url. 当然,当用户深度链接到该URL时,您需要自己处理正确状态的恢复。 You could do that in the activate method base on the params object 您可以在基于params对象的activate方法中执行此操作

activate(params){
    // params.relevantStateValue1
    // params.relevantStateValue2
    // etc
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM