【问题标题】:@Component giving error in angular2+@Component 在 angular2+ 中给出错误
【发布时间】:2018-05-07 20:36:01
【问题描述】:

我正在尝试在我的应用程序中使用微调器,因为有很多数据需要时间来加载。但这是问题所在。我已将此链接作为参考 -

Pre-Bootstrap Loading Screen For Angular2

to implement a spinner in angular2+

在上面显示的文章中,他们提到使用或(或可能同时)Angular 将在加载 Angular 后自动将“正在加载...”替换为真实内容。但是,这不适用于我的程序。 请检查我的代码并进行相应的指导。

错误

   Argument of type '{ selector: string; 'my-app': any; templateUrl: 
   string; styleUrls: string[]; }' is not assignable to parameter of 
   type 'Component'.
    Object literal may only specify known properties, and ''my-app'' 
   does not exist in type 'Component'.

transaction.component.html

 <table>
 <thead> ....
 </thead>

     <my-app>
      <div class="loaded" *ngIf="loaded">
      <div class="text-center">
       <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
        <div> Data Waiting To Be Loaded </div>
       </div>
      </div>
    </my-app>

  <tbody>
        .....
  </tbody>
   <table>

transaction.component.ts

   @Component({
    selector: 'app-transaction','my-app',
    templateUrl: './transaction.component.html',
    styleUrls: 
     ['./transaction.component.scss',]

     }) 


 export class TransactionComponent implements OnInit {
 public loaded=false;


 ngOnInit() {

 this.loaded=true;


  }

 insideSearchByText(filterObj) {

    this.http.post("http://" + url + ":" + port + 
  .....

   .map(result => this.result = result.json(),

    this.loaded=false)


    ....
    }});
    }

transaction.component.scss

  .loaded {
   display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
   }

【问题讨论】:

    标签: angular spinner


    【解决方案1】:
    @Component({
      selector: 'app-transaction',
      templateUrl: './transaction.component.html',
      styleUrls: 
        ['./transaction.component.scss',]
    })
    

    另一个组件(如果你有的话):

    @Component({
          selector: 'my-app',
          templateUrl: './my-app.component.html',
          styleUrls: 
            ['./my-app.component.scss',]
        })
    export class MyAppComponent {
    ...
    }
    

    在模板中使用该选择器:

    <my-app>
      <div class="loaded" *ngIf="loaded">
        <div class="text-center">
          <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
          <div> Data Waiting To Be Loaded </div>
        </div>
      </div>
    </my-app>
    

    如果您没有 MyAppComponent,则不能在模板中使用它。为什么模板中有 ?

    【讨论】:

    • 这不是循环依赖吗,因为打字稿部分会尝试渲染 html 并且会调用选择器,这会以无限循环结束?
    • 我想我不明白。 创建组件的一个实例。为什么会有无限循环或循环依赖?
    • 是的,但在这种情况下,您再次调用它,就像递归一样。还是我错了?你不能从它自己的html部分调用组件?
    • 啊。它在事务组件模板中。抱歉,我没有注意到这一点。我将编辑我的答案。
    • so laker .... 是另一个组件...并且它不是强制使用它...使微调器工作... ??
    【解决方案2】:

    我认为您的问题是语法错误。您已经为此组件定义了两个选择器。我认为您的 transaction.component.ts

    中的这一行
    selector: 'app-transaction','my-app',
    

    应该只是

    selector: 'app-transaction',
    

    因为选择器不将数组作为值(例如 styleUrls 那样)。

    【讨论】:

    • 那么如何确认“my-app”...我是否需要完全创建一个新组件才能在微调器中使用 --> transaction.component.html
    • 这个组件使用的标签是&lt;app-transaction&gt;
    • 我想知道我们如何在另一个组件的 html 中访问不同的组件(例如'my-app')
    • 您只需像在html 中那样使用它们。如果您有一个带有选择器的组件:'my-app',那么您可以使用它。你有没有这个选择器的组件?
    • 就像在这个链接中一样 --> danielykpan.github.io/date-time-picker .. 我想使用带有 rangeFrom 和 rangeTo 选择的 Picker .. 所以它有一个 ts 文件。所以要访问那个 datetimepicker.. 我是否必须创建另一个组件(如 ng g component datetimepicker)??
    猜你喜欢
    • 2017-01-17
    • 2016-08-07
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2016-12-10
    相关资源
    最近更新 更多