如何从 Blazor 上的 c# 代码制作 window.history.go(-1)?
我尝试使用 JSRuntime.Current.InvokeAsync
JSRuntime.Current.InvokeAsync< string >( "history.go", new[] {-1} );
JSRuntime.Current.InvokeAsync< string >( "top.history.go", new[] {-1} );
JSRuntime.Current.InvokeAsync< string >( "window.history.go", new[] {-1} );
JSRuntime.Current.InvokeAsync< string >( "window.top.history.go", new[] {-1} );
Run Code Online (Sandbox Code Playgroud)
但我收到错误:调用未实现接口历史记录的对象。
但它从 cshtml 工作:
<button id="BtnBack" type="button" onclick="window.history.go(-1)" class="btn btn-default">Back</button>
Run Code Online (Sandbox Code Playgroud)
不确定细节,但我相信像这样进行互操作,你需要更明确一点。所以如果你把这个脚本放在你的index.html(不是我一定建议你把它投入生产的地方):
<script>
window.test = {
historyGo(value) {
window.history.go(value);
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
然后将此代码放在您的 Blazor 页面中:
<button type="button" onclick="@OnClick">Go Back</button>
Run Code Online (Sandbox Code Playgroud)
使用这样的事件处理程序:
private void OnClick()
{
JSRuntime.Current.InvokeAsync<object>("test.historyGo", -1);
}
Run Code Online (Sandbox Code Playgroud)
一切都如您所愿。我尝试了您采用的方法(在您自己的 JS 中没有明确的互操作层),同样发现它不起作用。但这对我来说似乎是一个合理的解决方法。有兴趣详细说明为什么这有效而您的代码无效,但希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
2686 次 |
| 最近记录: |