我正在嘗試javascript在剃須刀組件中加載加載函式,但出現錯誤。
@inject IJSRuntime JsRuntime
@if (listProducts != null)
{
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Home place</th>
<th>Expiration</th>
<th>Phone number</th>
<th>Tea</th>
<th>Local</th>
<th>App</th>
<th>Company /Lease</th>
<th>Road time</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach(var product in this.listProducts)
{
<ProductItemComponent
Product="product"
OnProductDeleted="OnProductDeleted"></ProductItemComponent>
}
</tbody>
</table>
}
@code {
protected override async Task OnInitializedAsync()
{
await LoadProducts();
await JsRuntime.InvokeVoidAsync("exampleJsFunctions.DisableCopy");
}
}
一開始我是這樣注入IJSRuntime的@inject IJSRuntime JsRuntime,在OnInitializedAsync()我呼叫我的函式時,_Layout.cshtml它看起來像這樣
<script>
window.exampleJsFunctions =
{
DisableCopy: function (data) {
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = clickNS4;
}
else if (document.all && !document.getElementById) {
document.onmousedown = clickIE4;
}
}}
</script>
這是控制臺中的錯誤

關于有什么問題的任何想法?
uj5u.com熱心網友回復:
這是一個關于在 blazor 中呼叫 js 的演示:
wwwroot/js/test.js(將js代碼放入wwwroot的js檔案):
window.exampleJsFunctions =
{
DisableCopy: function (data) {
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = clickNS4;
}
else if (document.all && !document.getElementById) {
document.onmousedown = clickIE4;
}
}}
Pages/_Host.cshtml(在_Host.cshtml中添加test.js的參考):
...
<html lang="en">
<head>
...
</head>
<body>
...
<script src="~/js/test.js"></script>
</body>
</html>
不要在里面呼叫js OnInitializedAsync()。組件是靜態渲染的。所以此時無法發出JavaScript互操作呼叫。所以你可以在里面呼叫它OnAfterRender。
剃須刀組件:
@inject IJSRuntime JsRuntime
@code {
protected override void OnAfterRender(bool firstRender)
{
JsRuntime.InvokeVoidAsync("exampleJsFunctions.DisableCopy");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444887.html
標籤:javascript asp.net 核心 剃刀
上一篇:SamQueryInformationDomain:使用_DOMAIN_PASSWORD_INFORMATION獲取DOMAIN_PASSWORD_COMPLEX
