无法在Stackblitz中调用Angular(v15)子组件。

huangapple go评论65阅读模式
英文:

Could not call Angular (v15) child component in Stackblitz

问题

我在运行 Angular 的笔记本上调用子组件时没有问题。
我相信这是版本 12。

通常情况下,我会从默认应用组件中调用子组件。

但是在 StackBlitz 中,它只有一个 index.html,所以我从那里调用子组件。

这是 main.ts 文件。

import 'zone.js/dist/zone';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
import { InputButtonUnitComponent } from './input-button-unit/input-button-unit.component';


@Component({
  selector: 'my-app',
  standalone: true,
  imports: [CommonModule],
  template: `
    <h1>Hello from {{name}}!</h1>
    <a target="_blank" href="https://angular.io/start">
      Learn more about Angular 
    </a>
    <br>
    <p> Hello </p>
    <label>Enter </label>
    <input>
    <button>click me</button>
  `,
})
export class App {
  name = 'Angular';
}

bootstrapApplication(App);

我在 index.html 中添加了子组件。

<html>
  <head>
    <title>My app</title>
  </head>
  <body>
    <my-app>Loading...</my-app>
    <app-child></app-child>
  </body>
</html>

附上一张图片,希望清楚地显示了问题。
无法在Stackblitz中调用Angular(v15)子组件。

提前感谢!

英文:

I have no issue calling a child component on a notebook running Angular.
I believe it is version 12.

Normally I will call the child component from the default app component.

But in StackBlitz it only has index.html, so I call the child component
from there.

This is the main.ts.

import &#39;zone.js/dist/zone&#39;;
import { Component } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { bootstrapApplication } from &#39;@angular/platform-browser&#39;;
import {  InputButtonUnitComponent } from &#39;./input-button-unit/input-button-unit.component&#39;;


@Component({
  selector: &#39;my-app&#39;,
  standalone: true,
  imports: [CommonModule],
  template: `
    &lt;h1&gt;Hello from {{name}}!&lt;/h1&gt;
    &lt;a target=&quot;_blank&quot; href=&quot;https://angular.io/start&quot;&gt;
      Learn more about Angular 
    &lt;/a&gt;
    &lt;br&gt;
    &lt;p&gt; Hello &lt;/p&gt;
    &lt;label&gt;Enter &lt;/label&gt;
    &lt;input&gt;
    &lt;button&gt;click me&lt;/button&gt;
    
  `,
})
export class App {
  name = &#39;Angular&#39;;
}

bootstrapApplication(App);

I added the component at the index.html.

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;My app&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;my-app&gt;Loading...&lt;/my-app&gt;
    &lt;app-child&gt;&lt;/app-child&gt;
  &lt;/body&gt;
&lt;/html&gt;

Attached is an image which hopefully shows the issue clearly.
无法在Stackblitz中调用Angular(v15)子组件。

Thanks in advance!

答案1

得分: 0

app-child 不在你的引导应用程序内。

请执行以下操作之一:

  • app-child 移动到 App 的模板内。
  • app-child 启动第二个Angular应用程序。
英文:

Your app-child is not within your bootstrapped application.

Will either :

  • move app-child within the template of App
  • bootstrap an second angular app for app-child

huangapple
  • 本文由 发表于 2023年3月7日 15:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定