Angular 程序菜单组件模板不起作用

标签 angular angular-cli

我正在尝试在 Angular 6 中添加一个菜单组件。一切正常,但菜单组件模板除外。
问题是浏览器给出错误:

Failed to compile.

./src/app/menu/menu.component.ts
Module not found: Error: Can't resolve './menu/menu.component.html' in '/home/fly/myangular/conFusion/src/app/menu'

menu.component.ts 中,我在组件装饰器中有 templateUrl 地址。

@Component({
  selector: 'app-menu',
  template: `
    <p>
      menu worksx!
    </p>
  `,
  styles: [],
  templateUrl: './menu/menu.component.html',
  styleUrls: ['./menu/menu.component.scss']
})  

这是我的 menu.component.html View :

<div class="container"
     fxLayout="column"
     fxLayoutGap="10px">

  <mat-list fxFlex>
    <mat-list-item *ngFor="let dish of dishes">
      <img matListAvatar src={{ dish.image }} alt={{ dish.name }}>
      <h1 matLine> {{ dish.name }} </h1>
      <p matLine>
        <span> {{ dish.description }} </span>
      </p>
    </mat-list-item>
  </mat-list>

</div>

PS:这是我的项目结构:
enter image description here


编辑: 修改模板地址后,页面变白, 这是我的控制台:

MenuComponent.html:7 ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '}}' is not a valid attribute name.
    at EmulatedEncapsulationDomRenderer2.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.DefaultDomRenderer2.setAttribute (http://localhost:4200/vendor.js:83639:16)
    at BaseAnimationRenderer.push../node_modules/@angular/platform-browser/fesm5/animations.js.BaseAnimationRenderer.setAttribute (http://localhost:4200/vendor.js:82265:23)
    at DebugRenderer2.push../node_modules/@angular/core/fesm5/core.js.DebugRenderer2.setAttribute (http://localhost:4200/vendor.js:52472:23)
    at createElement (http://localhost:4200/vendor.js:49144:22)
    at createViewNodes (http://localhost:4200/vendor.js:51370:26)
    at createEmbeddedView (http://localhost:4200/vendor.js:51317:5)
    at callWithDebugContext (http://localhost:4200/vendor.js:52354:25)
    at Object.debugCreateEmbeddedView [as createEmbeddedView] (http://localhost:4200/vendor.js:51855:12)
    at TemplateRef_.push../node_modules/@angular/core/fesm5/core.js.TemplateRef_.createEmbeddedView (http://localhost:4200/vendor.js:49904:38)
    at ViewContainerRef_.push../node_modules/@angular/core/fesm5/core.js.ViewContainerRef_.createEmbeddedView (http://localhost:4200/vendor.js:49770:35)
View_MenuComponent_1 @ MenuComponent.html:7
MenuComponent.html:7 ERROR CONTEXT DebugContext_
View_MenuComponent_1 @ MenuComponent.html:7
core.js:3121 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
%7B%7B:1 Failed to load resource: the server responded with a status of 404 (Not Found)

这是我的app.module.ts:

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';


import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatToolbarModule } from '@angular/material/toolbar'; 
import { FlexLayoutModule } from '@angular/flex-layout';

import 'hammerjs';
import { MenuComponent } from './menu/menu.component';

import { MatListModule } from '@angular/material/list';
@NgModule({
  declarations: [
    AppComponent,
    MenuComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MatToolbarModule,
    FlexLayoutModule,
    MatListModule
  ],
  providers: [],
  bootstrap: [AppComponent ]
})
export class AppModule { }

最佳答案

组件应该是这样的:(没有箭头)

@Component({
  selector: 'app-menu',
  templateUrl: 'menu.component.html',
  styleUrls: ['menu.component.scss']
}) 

关于Angular 程序菜单组件模板不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52829310/

相关文章:

css - 可清除输入覆盖标签填充

javascript - Angular、可观察的订阅 promise

Angular react 形式 - 当用户将 UI 更改恢复为原始值时,原始值是否会重置?

angular - Base-href/deploy-url 参数未按预期工作 : paths aren't changed after building

angular - 如何避免 Angular-cli 中的着色?

angular - 无法在 angular-cli 项目中使用 npm 包含 Bootstrap

angular - 除了设计简单之外,在 Angular2 中使用多个模块还有什么好处?

node.js - 在集合 "library"中找不到原理图 "@schematics/angular"

Angular CLI 错误 : The serve command requires to be run in an Angular project, 但找不到项目定义

javascript - 从可观察的 HTTP 请求的订阅中为 Angular 组件中的变量赋值