简体   繁体   English

无法在angular6中使用键值为'templateUrl'的对象

[英]Unable to use the object with key value 'templateUrl' in angular6

I have the below object in component.ts and I am going to iterate this array in the HTML template inside ui-comp-menu. 我在component.ts中有以下对象,并且要在ui-comp-menu的HTML模板中迭代此数组。

menuObject = [{
        'labels': 'Content1',
        'templateUrl': 'assets/partials/sample.html',
        'childrens': [],
    },
    {
        'labels': 'Content2',
        'templateUrl': 'assets/partials/sample1.html',
    }];

and in the html 并在html中

<ui-comp-menu [data]="menuObject" [label]="'labels'" [url]="'routeUrl'" ></ui-comp-menu>

But while compiling I got the below error 但是在编译时出现以下错误

ERROR in ./src/app/app.component.ts ./src/app/app.component.ts中的错误

Module not found: Error: Can't resolve './assets/partials/UserAdministration.html' in 'D:\\POC_Angular_6\\lib-demo\\src\\appI 找不到模块:错误:无法解析'D:\\ POC_Angular_6 \\ lib-demo \\ src \\ appI中的'./assets/partials/UserAdministration.html'

If i change the object key 'templateUrl' to some other name like 'templatePath' issue won't come and compiled successfully. 如果我将对象键'templateUrl'更改为其他名称,例如'templatePath',则问题不会成功编译。 But i am not sure about previous angular versions. 但是我不确定以前的角度版本。

This is a bug from the angular-cli package. 这是angular-cli软件包中的错误。 When you check the source code you can see that they are parsing the entire .ts file and subtracting any object in there. 当您检查source code您会看到它们正在解析整个.ts文件并减去其中的任何对象。 Then they are checking if there is an object with a templateUrl or styleUrls key in there. 然后,他们正在检查其中是否存在带有templateUrlstyleUrls键的对象。

This seems a bit erratic, and could be done more properly. 这似乎有些不稳定,可以更适当地完成。 For instance to check if the subtracted object is inside an annotation ( @Component ). 例如,检查被减去的对象是否在注释( @Component )内。

Couple things you can do here: 您可以在这里做几件事情:

  • Submit a bug report, wait for them to fix it, and in the meantime replace the key with something else 提交错误报告,等待他们进行修复,与此同时,将密钥替换为其他内容
  • Make your own pull request, wait for them to accept it, and etc... 提出您自己的拉取请求,等待他们接受,等等。
  • You say that the data comes from an API, if that's the case, you don't really need to have those hard coded templateUrl definitions inside your typescript file. 您说数据来自API,如果是这种情况,您实际上不需要在打字稿文件中包含那些硬编码的templateUrl定义。 Connecting to the API and removing those from your code should be enough. 连接到API并将它们从代码中删除就足够了。
  • Change the key from the API and just keep it at that 更改API中的密钥并保持不变

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

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