Refer:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components
The component of core and Angular are different.
The component of core is just an upgrade to partial view.
The former partial view is just a view package, and component is the Controller + view package
So convenient for some.
First define a class inheritance Viewcomponent, component's name is class name, of course, we can change it,
[Area ("Web")][viewcomponent (Name="keatkeat")]//Modify name Public classmyheader:viewcomponent{PrivateDB DB {Get;Set; } PublicMyHeader (db db) { This. db =DB; } PublicTask<iviewcomponentresult> InvokeAsync (stringvalue)//can be passed at the time of invocation Oh {varVM =NewViewModel {Name=value}; returnTask.fromresult<iviewcomponentresult>(View (VM)); }}
Call
@addTagHelper *, Project
@{var value = "keatkeat87";} @await Component.invokeasync ("Keatkeat", value) @await Component.invokeasync (nameof ( Project.Web.ViewComponents.MyHeader.MyHeader), value)<value= "@value" ></vc:keatkeat>
There are 3 ways to call it.
The first is to write component names.
The second is to find the class through nameof, if you change the name, then the call will be broken.
The third is through the element mode (need to add @addTagHelper in _viewimports.cshtml *, Project, project is the name of my project, directly put the entire project assembly on it)
The viewcomponent template differs from the general controller lookup, and the general controller View name is index.cshtml
and Viewcomponent is components/componentname/default.cshtml.
ASP. NET Core Learning Notes (Viewcomponent components)