图表组件应该接受一些输入参数并显示 VueJS 渲染的图表。Blazor 不允许script
在文件中添加标签*.razor
,所以我*.cshtml
改用它。
/Pages/Index.razor - 主页
@page "/"
<ChartComponent></ChartComponent>
/Shared/ChartComponent.cshtml - 要在页面上显示的组件
<div id="charts" style="width: 300px; height: 300px; border: 1px solid red;">
<chart-control :data="bars"></chart-control>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="~/scripts/vue-charts/library/trading-vue.js"></script>
<script>
window.bars = {
ohlcv: [
[1551128400000, 33, 37.1, 14, 14, 196],
[1551132000000, 13.7, 30, 6.6, 30, 206],
[1551135600000, 29.9, 33, 21.3, 21.8, 74],
[1551139200000, 21.7, 25.9, 18, 24, 140],
[1551142800000, 24.1, 24.1, 24, 24.1, 29],
]
};
window.onload = function () {
var vm = new Vue({
el: '#charts',
components: { 'chart-control': window.TradingVueLib.TradingVue }
});
};
</script>
/_Import.razor
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using Charts
@using Charts.Shared
问题
如果我将 JS 放入 Host.cshtml,一切正常,但是当我尝试将其设为单独的 CSHTML 组件时,主页ChartComponent
会抱怨未定义。CSHTML 可以是一个组件还是我需要将它作为老式 MVC 部分包含在内?