您好,我退出了本指南,将asp.net核心依赖注入集成到MVC 5中。最初,它使用默认控制器(从System.Web.Mvc.Controller类继承)可以正常工作。但是后来我希望注入的依赖项在从继承的控制器中可用System.Web.Http.ApiController。我有95%的把握是问题出在代码的这一部分,所有的控制器都作为服务添加了,因为我遇到的错误与指南中所说的相同,我会在没有该部分的情况下得到错误。
缺少的方法异常,说明您的构造函数未实现默认的无参数构造函数
public static class ServiceProviderExtensions
{
public static IServiceCollection AddControllersAsServices(this IServiceCollection services,
IEnumerable<Type> controllerTypes)
{
foreach (var type in controllerTypes)
{
services.AddTransient(type);
}
return services;
}
}
public partial class Startup
{
public void ConfigureServices(IServiceCollection services){
PackageScraperService pScraper = new PackageScraperService();
services.addSington<IPackageScraper>(pScraper);
// Problem Code
services.AddControllersAsServices(typeof(Startup).Assembly.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
.Where(t => typeof(IController).IsAssignableFrom(t)
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
}
public void Configuration(IAppBuilder app){
var services = new ServiceCollection();
ConfigureServices(services)
var resolver = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 BootrapVue 同时使用 buefy 的 b-table 组件。我猜 BootstrapVue 也有一个名为 b-table 的组件,它会导致冲突并导致 buefy 的表无法正常工作。当我删除 BootstrapVue 时,Buefy 表会正确显示。我是 vue 新手,我不确定如何解决这样的命名空间冲突,或者是否可能。
main.js
import BootstrapVue from 'bootstrap-vue';
Vue.use(BootstrapVue);
import Buefy from 'buefy';
import 'buefy/dist/buefy.css';
Vue.use(Buefy)
Run Code Online (Sandbox Code Playgroud)
元数据表.js
<template>
<div>
<!-- BootstrapVue -->
<b-modal id="metadata-modal" title="Metadata" size="lg">
<p class="my-4">
<table style="text-align:left">
<tbody>
<tr v-for="(value,key) in current_metadata">
<td style="vertical-align:top;font-weight:bold;">{{key}} </td>
<td><code><pre>{{JSON.stringify(value, null, 2)}}</pre></code></td>
</tr>
</tbody>
</table>
</p>
</b-modal>
<!-- Buefy -->
<b-table
:data="data"
paginated
>
<template slot-scope="props">
<b-table-column field="metadata.title" label="Title" sortable>
{{ props.row.metadata.title }}
</b-table-column>
</template>
</b-table>
</div> …Run Code Online (Sandbox Code Playgroud) 我只想items从数据库中获取一次(我不希望它们改变).当我最初加载home-page.component.ts时,HomePageComponent.items获取正确设置但是如果我导航HomePageComponent到ProfilePageComponent然后返回到HomePageComponent因为itemsService.items$已经解决了订阅回调没有重新运行并且HomePageComponent.items没有重新初始化.这是我正在使用的当前解决方法,但是处理这种情况的正确方法感觉太笨重了.
家庭page.component.ts
ngOnInit() {
this.itemsService.items$.subscribe((items:item) => {
// Is only called once after items are initalially fetched over http
this.items = items;
this.itemsService.addItems(items);
});
if(this.itemsService.foundItems){
this.items= this.itemsService.items;
}
}
Run Code Online (Sandbox Code Playgroud)
items.service.ts
export class ItemsService {
private items = new BehaviorSubject(null);
items$ = this.items.asObservable();
foundItems = null;
constructor(private httpClient: HttpClient){
this.items.next(httpClient.post<any>(myUrl,myQuery).subscribe(this.items));
}
addItems(items){
this.foundItems = items;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将数据异步加载到静态的vuex中,但该vuex被多个路由使用。我只想一次且仅在访问需要它的路由时才获取数据。这是我目前正在做的事情,但是我不确定这是否是正确的约定,或者是否有更好/更多的Vueish方法。
// store.js
export default new Vuex.Store({
state: {
_myData: null,
},
getters: {
myData: (state) => new Promise((resolve,reject) => {
if(state._myData){
resolve(state._myData);
} else {
axios.get('http://myData.com/')
.then(myData => {
state._myData = myData;
resolve(state._myData);
});
}
})
}
});
// ProfilePage.vue
<template>
<div>{{myData}}</div>
</template>
<script>
export default {
data() {
return {
myData:null
}
},
async created(){
this.myData = await this.$store.getters.myData;
}
}
</script>
// AboutPage.vue
<template>
<div>{{myData}}</div>
</template>
<script>
export default {
data() {
return {
myData:null
}
}, …Run Code Online (Sandbox Code Playgroud) vue.js ×2
vuejs2 ×2
angular ×1
asp.net ×1
asp.net-mvc ×1
buefy ×1
c# ×1
observable ×1
rxjs ×1
vuex ×1