Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Blazor client index.html中加载JavaScript文件_Javascript_Blazor_Blazor Webassembly - Fatal编程技术网

如何在Blazor client index.html中加载JavaScript文件

如何在Blazor client index.html中加载JavaScript文件,javascript,blazor,blazor-webassembly,Javascript,Blazor,Blazor Webassembly,我在.js文件中有以下JavaScript代码: export function testAlert() { alert('Testing Blazor'); } 当我将此添加到剃须刀组件时,我得到一个警告: 我在index.html文件中添加了以下内容-根据建议: <head> <meta charset="utf-8" /> <meta name="viewport" content="width

我在.js文件中有以下JavaScript代码:

export function testAlert() {
  alert('Testing Blazor');
}
当我将此添加到剃须刀组件时,我得到一个警告:

我在index.html文件中添加了以下内容-根据建议:

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title>BlazorTest</title>
  <base href="/" />
  <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
  <link href="css/app.css" rel="stylesheet" />
  <link href="BlazorTest.styles.css" rel="stylesheet" />
  <script type="text/javascript" src="scripts/testing.js"></script>
</head>

BlazorTest
相应地修改了组件:

@inject IJSRuntime JS

protected override async Task OnAfterRenderAsync(bool firstRender)
{
  base.OnAfterRenderAsync(firstRender);
  await JS.InvokeAsync<string>("testAlert");
}
@inject IJSRuntime JS
AfterRenderAsync(bool firstRender)上受保护的重写异步任务
{
base.OnAfterRenderAsync(firstRender);
等待JS.InvokeAsync(“testAlert”);
}
加载页面时,出现以下错误:

未处理的异常呈现组件:找不到“testAlert”(“testAlert”未定义)

我检查了页面的来源,它看起来像这样:

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title>BlazorTest</title>
  <base href="/" />
  <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
  <link href="css/app.css" rel="stylesheet" />
  <link href="BlazorTest.styles.css" rel="stylesheet" />
</head>

BlazorTest

使用加载的脚本修改的index.html发生了什么情况?

有时浏览器会缓存index.html文件。这里可能就是这样


通常,使用Ctrl-F5强制刷新将强制下载新编辑的文件。

有时index.html文件由浏览器缓存。这里可能就是这样。清除缓存后是否尝试重新加载页面?Ctrl-F5通常为我做。我认为如果你从函数中删除导出,并确保你已经保存了对index.html的更改,你的代码应该可以工作。使用JS或TS中的“导出”将“编译”到一些时髦的代码,甚至可以重新实例化对象。我个人非常不喜欢它。无论如何,删除“导出”可能会解决这个问题,否则-看看您在浏览器devtools中实际得到了什么,可能您实际得到了一层对象,您可能需要在JS Interop中匹配这些名称空间call@EricKing对看起来它被大量缓存了:我添加了一些HTML代码,在按下Ctrl+F5键之前,更改是不可见的。奇怪的是,下一次编辑不需要它,并立即刷新。把你的评论提升到一个答案,我会接受的。