除非序列化为JSON,否则将OBJ传递给JS空的obj。其他对象有效
从本质上讲,我只能将原始类型和匿名类型作为与JS Interop Interop Interop中的参数传递。
我在堆栈溢出上的第一篇文章是因为我无法弄清楚这一点:
此示例有效:
C#:
async void doThing()
{
await JSRuntime.InvokeAsync<object>("test", new { Name = "John", Age = 35 });
}
JS:
var test = funciton(stuff){
console.log(stuff);
return stuff;
}
Output in JS is object with name and age as expected.
现在是我的实际班级:
public class CarsAndBikes
{
public car[] cars;
public bike[] bikes;
}
async void doThing()
{
await JSRuntime.InvokeAsync<object>("test", carsAdnBikes);
}
它不是那么复杂,而是我在JS中看到的是一个空的对象,除非我将其序列化为JSON和挑战。汽车和自行车是课程。 C#
JsonConvert.SerializeObject(CarsAndBikes);
JS:
JSON.parse(stuff)
然后可以正常工作。我不知道为什么我不能将我的班级交给JS,有人知道我是在做错事还是一个错误?
Essentially I can only pass primitives and anonymous types as argument in Blazor interop with JS.
My first post on stack overflow ever because I just cant figure this out:
This example works:
C#:
async void doThing()
{
await JSRuntime.InvokeAsync<object>("test", new { Name = "John", Age = 35 });
}
JS:
var test = funciton(stuff){
console.log(stuff);
return stuff;
}
Output in JS is object with name and age as expected.
Now here's my actual class:
public class CarsAndBikes
{
public car[] cars;
public bike[] bikes;
}
async void doThing()
{
await JSRuntime.InvokeAsync<object>("test", carsAdnBikes);
}
Its not that complex but what I can see in JS is an empty object, unless I serialise it as JSON and deserialise. car and bike are classes.
C#
JsonConvert.SerializeObject(CarsAndBikes);
JS:
JSON.parse(stuff)
Then it works fine. I have no idea why I cannot pass my class to JS, does anyone know if Im doing something wrong or its a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个使用您的代码的简单工作演示。它有效,因此您没有向我们展示会破坏您的代码。
CAR
和自行车
看起来像什么?Here is a simple working demo using your code. It works, so there's something your not showing us that breaks your code. What does
Car
andBike
look like?