首页 > Web开发 > 详细

关于ASP NET Core的视图组件的使用(ViewComponent)

时间:2021-08-13 14:42:00      阅读:9      评论:0      收藏:0      [点我收藏+]

问:

想请教一下关于视图组件在父页面中的占位问题
·
1、直接在父页面中使用<vc>标签占位(明确知道了视图组件在父页面中的位置)
<div id="first">
······<p> hello world </p>
··
······<vc:priority-list max-priority="2" is-done="false">
······</vc:priority-list>
</div>
2、从控制器直接调用视图组件(如何确定视图组件在父页面的位置??)

public IActionResult IndexVC()
{
return ViewComponent("组件类名", (参数)new { maxPriority = 3, isDone = false });
}

 

答:

从 Controller 中直接返回 ViewComponent,返回结果是渲染好的 html,你可以通过 js 自行放到你想要的位置。

比如你有一个 ViewComponent,返回一行字 <p>view component</p>

访问 /IndexVC 这个路径,返回的就是 <p>view component</p>

你可以通过 js 拿到这段 html 代码放到你需要的位置。

<div id="first">
    <p> hello world </p>
    <div id="placeholder">
        <script>
            fetch(/IndexVC).then(res => res.text().then(text => document.getElementById(placeholder).innerHTML = text)) 
        </script>
    </div>
</div>

来自:https://q.cnblogs.com/q/133219/

关于ASP NET Core的视图组件的使用(ViewComponent)

原文:https://www.cnblogs.com/djd66/p/15136901.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!