我想在加载blazor-wasm应用程序时显示引号。如何访问此消息类。我已经把脚本放在index.html的头标记中了。但是不能访问这个?
<script>
fetch("https://localhost:44359/data/Quotes.json")
.then(res => res.json())
.then(data => console.log(data.quotes));
document.querySelector("body").innerHTML = "hey";
</script>
</head>
<body>
<app>
<div class="loading-page">
<div class="quotes">
<div class="msg"></div>
<div class="author"></div>
</div>
<div class="loader mt-5">
<div class="loader-dot dot1"></div>
<div class="loader-dot dot2"></div>
<div class="loader-dot dot3"></div>
</div>
</div>
</app>
<script src="_framework/blazor.webassembly.js"></script>
<script>
navigator.serviceWorker.register("service-worker.js");
</script>
</body>
有什么办法吗???
这就是你应该做的:
<body>
<app><div id="message"></div></app>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
<script>
(function () {
var quote = document.getElementById("message");
fetch("sample-data/quotes.json")
.then(res => res.json())
.then(data => quote.innerHTML = data[1].msg + "<br /><br />" + data[1].author);
})();
</script>
</body>
在wwwroot文件夹中创建一个名为sample data的文件夹,并在其中创建一个包含以下数据的json文件:
quotes.json
[
{
"msg": "It is a far, far better thing that I do, than I have ever done; it is a far, far better rest I go to than I have ever known.",
"author": "Charles Dickens"
},
{
"msg": "All we have to decide is what to do with the time that is given us.",
"author": "J.R.R. Tolkein"
},
{
"msg": "It matters not what someone is born, but what they grow to be.",
"author": "J.K. Rowling"
}
]
注意:你可以在应用程序元素中插入你想要的任何标记,包括图像、文本滑块等,并进行你喜欢的任何操作。。。然而,重要的是,所有代码都应该在immediate函数中,位于它在示例中的相同位置。