简体   繁体   English

Angular 2,模板错误打破了简单的网站

[英]Angular 2, template error breaks simple site

I'm trying to modify the code from a NG2 lesson to make a dynamic spotify player in NG2. 我正在尝试修改NG2课程中的代码,以使NG2中的动态Spotify播放器成为可能。 When I add <h1>anything</h1> to my template in app.component.js it works. 当我在app.component.js的模板中添加<h1>anything</h1> ,它都可以工作。 When I add in + ` the code gives this error 当我添加+`时,代码给出此错误

zone.min.js:1 Unhandled Promise rejection: Template parse errors:
'spotifyiframeplayer' is not a known element:
1. If 'spotifyiframeplayer' is an Angular component, then verify that it is part of this module.
2. If 'spotifyiframeplayer' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("<h1>Spotify Iframe Player</h1>[ERROR ->]<spotifyiframeplayer></spotifyiframeplayer>"): AppComponent@0:30 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
'spotifyiframeplayer' is not a known element:
1. If 'spotifyiframeplayer' is an Angular component, then verify that it is part of this module.
2. If 'spotifyiframeplayer' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("<h1>Spotify Iframe Player</h1>[ERROR ->]<spotifyiframeplayer></spotifyiframeplayer>"): AppComponent@0:30
    at TemplateParser.parse (https://unpkg.com/@angular/compiler@2.0.0/bundles/compiler.umd.js:8530:21)
    at RuntimeCompiler._compileTemplate (https://unpkg.com/@angular/compiler@2.0.0/bundles/compiler.umd.js:16905:53)
    at https://unpkg.com/@angular/compiler@2.0.0/bundles/compiler.umd.js:16828:85
    at Set.forEach (native)
    at compile (https://unpkg.com/@angular/compiler@2.0.0/bundles/compiler.umd.js:16828:49)
    at e.invoke (https://unpkg.com/zone.js@0.6.21/dist/zone.min.js:1:15936)
    at n.run (https://unpkg.com/zone.js@0.6.21/dist/zone.min.js:1:13323)
    at https://unpkg.com/zone.js@0.6.21/dist/zone.min.js:1:11425
    at e.invokeTask (https://unpkg.com/zone.js@0.6.21/dist/zone.min.js:1:16565)
    at n.runTask (https://unpkg.com/zone.js@0.6.21/dist/zone.min.js:1:13925)o @ zone.min.js:1a @ zone.min.js:1a @ zone.min.js:1
zone.min.js:1 Error: Uncaught (in promise): Error: Template parse errors:(…)o @ zone.min.js:1a @ zone.min.js:1a @ zone.min.js:1

Link to Plunker I'm not sure how to fix this error. 链接到Plunker我不确定如何解决此错误。 I have checked and the element is only mentioned one time in the sample code and works for that project. 我已经检查过,并且在示例代码中仅一次提到了该元素,并且该元素适用于该项目。

@galvon (this is also on the plunker) @galvon(这也位于punker上)

(function(app) {
  var Component = ng.core.Component;

  app.AppComponent = Component({
    selector: 'my-app',
    template:
      `
      <h1>Spotify Iframe Player</h1>
      <spotifyiframeplayer></spotifyiframeplayer>
      `
  })
  .Class({
    constructor: function AppComponent() { }
  });

})(window.app || (window.app = {}));

don't use + to append strings. 不要使用+附加字符串。

to use multiline HTML , you can use backtick (find in your keyboard - ` ) as shown below, 使用多HTML,你可以使用反引号 (发现你的键盘- `),如下图所示,

template:
      '<h1>Spotify Iframe Player</h1>' +
      '<spotifyiframeplayer></spotifyiframeplayer>'

change it to 更改为

template:
      `
      <h1>Spotify Iframe Player</h1>
      <spotifyiframeplayer></spotifyiframeplayer>
      `

NOTE : Other than this, your plunker is incomplete so even after this change don't expect plunker to run as you don't have spotifyiframeplayer component implementation. 注意 :除此之外,您的punker是不完整的,因此即使在进行此更改后,也不要指望punker可以运行,因为您没有spotifyiframeplayer组件实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM