浏览文章

文章信息

Magento2 Uncaught Error: Cannot instantiate interface|WebApi开发 13778

1、报错

Uncaught Error: Cannot instantiate interface ***

2、原因

di.xml

文件中未注入实现类。

例如报错:

Uncaught Error: Cannot instantiate interface Aiweline\Blog\Api\TagManagementInterface

是因为你没有在di.xml文件中注入TagManagementInterface接口类

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Mjd\Blog\Api\PostManagementInterface" type="Mjd\Blog\Model\PostManagement" />
   <preference for="Mjd\Blog\Api\UserManagementInterface" type="Mjd\Blog\Model\User\UserManagement" />
   <preference for="Mjd\Blog\Api\CategoryManagementInterface" type="Mjd\Blog\Model\CategoryManagement" />
</config>


3、解决:

向di.xml文件中注入依赖

例子:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Aiweline\Blog\Api\PostManagementInterface" type="Aiweline\Blog\Model\PostManagement" />
   <preference for="Aiweline\Blog\Api\UserManagementInterface" type="Aiweline\Blog\Model\User\UserManagement" />
   <!--注入Aiweline\Blog\Api\TagManagementInterface以及实现类Aiweline\Blog\Model\User\TagManagement-->
   <preference for="Aiweline\Blog\Api\TagManagementInterface" type="Aiweline\Blog\Model\User\TagManagement" />
     <!--注入Aiweline\Blog\Api\TagManagementInterface以及实现类Aiweline\Blog\Model\User\TagManagement-->
   <preference for="Aiweline\Blog\Api\CategoryManagementInterface" type="Aiweline\Blog\Model\CategoryManagement" />
</config>



原创