【问题标题】:Angular `12.1.2 error NG8003: No directive found with exportAs 'ngForm'Angular `12.1.2 错误 NG8003:没有找到 exportAs 'ngForm' 的指令
【发布时间】:2021-10-01 18:15:03
【问题描述】:

我最近开始探索 Angular 并希望使用表单。

问题如标题所述。这是我的创建主题模板。

<form  #formData="ngForm" (ngSubmit)="createTheme(formData)">
            <div class="new-theme-title">
                <label for="themeName">Title <span class="red">*</span></label>
                <input type="text" name="themeName" id="themeName" >
                <p class="error">
                    Theme name is required.
                </p>
                <p class="error">
                    Theme name must be at least 5 characters long.
                </p>
            </div>
            <div class="new-theme-content">
                <label for="postText">Post <span class="red">*</span></label>
                <textarea type="text" name="postText" id="postText" rows="8" class="height" ></textarea>
                <p class="error">
                    The field with your post is required.
                </p>
                <p class="error">
                    Post must be at least 10 characters long.
                </p>
            </div>
            <div class="new-theme-buttons">
                <button class="cancel">Cancel</button>
                <button class="public">Post</button>
            </div>
        </form>

我尝试了什么?

    • 我已将import { NgForm } from '@angular/forms'; 导入到我的创建主题组件中。
    • 我已将ReactiveFormsModule, FormsModule 导入到我的主题模块中,并导入到根目录中。
    • 我已将FormsModule 导入到根模块中。

我不知道该怎么做。我的测试项目有一点简单的形式,它的结构和这里一样。它有效。

【问题讨论】:

    标签: angular typescript angular-directive


    【解决方案1】:

    经过数百篇帖子,我找到了解决方案:

    将 create-theme 组件导入 App 模块修复了一些事情。但我不知道为什么。

    【讨论】:

    • 那是因为如果组件不是模块的一部分,那么它就不会从模块的导入中受益
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2017-11-08
    • 2018-11-20
    • 2018-05-12
    • 2017-01-26
    • 2016-12-03
    • 2019-02-21
    • 2018-08-05
    相关资源
    最近更新 更多