简体   繁体   English

templateUrl中角度为4的相对URL且帆不起作用

[英]Relative urls in templateUrl with angular 4 and sails not working

In my app.component.ts I have to define the templateUrl as this: 在我的app.component.ts我必须这样定义templateUrl

import {Component} from '@angular/core';
@Component({
  selector: 'root-component',
  templateUrl: './app/app.component.html'
})
export class AppComponent { }

If I define it simply as ./app.component.html it gives this error in console: 如果我仅将其定义为./app.component.html则会在控制台中显示以下错误: 在此处输入图片说明

So basically this means that the app.component.html is accebile from http://localhost:1337/app/app.component.html because my sailsroot/asset folder structure is like this: 因此基本上,这意味着app.component.html可从http:// localhost:1337 / app / app.component.html进行升级,因为我的sailsroot/asset文件夹结构如下:

在此处输入图片说明

It would be very inconvenient to write templateUrls this way because for example in the sidebar component inside asset/app/sidebar folder the templateUrl would be written like: ./app/sidebar/sidebar.component.html 用这种方式编写templateUrl非常不方便,因为例如在asset/app/sidebar文件夹内的sidebar组件中, templateUrl编写方式如下: ./app/sidebar/sidebar.component.html asset/app/sidebar

I would like the relative urls in templateUrl to be resolved properly and I am not sure how to do that. 我想正确解析templateUrl中的相对URL,但我不确定该怎么做。

If it helps, I am following this tutorial. 如果有帮助,我正在关注教程。 (all my tsconfig.json, package.json etc.. are same as given here). (我所有的tsconfig.json,package.json等。都与此处给出的相同)。

You can try to add moduleId to the decorator like this : 您可以尝试将moduleId添加到装饰器中,如下所示:

@Component({
  moduleId: module.id,
  selector: 'root-component',
  templateUrl: './app/app.component.html'
})

However, according to angular.io change log, you should use relative path and you don't have to use moduleId, so I'm surprised your code doesn't work. 但是,根据angular.io更改日志,您应该使用相对路径,而不必使用moduleId,因此我很惊讶您的代码无法正常工作。

All mention of moduleId removed. 所有提及的moduleId均已删除。 "Component relative paths" cookbook deleted (2017-03-13) 删除了“组件相对路径”食谱(2017-03-13)

We added a new SystemJS plugin (systemjs-angular-loader.js) to our recommended SystemJS configuration. 我们在建议的SystemJS配置中添加了一个新的SystemJS插件(systemjs-angular-loader.js)。 This plugin dynamically converts "component-relative" paths in templateUrl and styleUrls to "absolute paths" for you. 该插件为您动态地将templateUrl和styleUrls中的“相对于组件”的路径转换为“绝对路径”。

We strongly encourage you to only write component-relative paths. 我们强烈建议您仅编写组件相对路径。 That is the only form of URL discussed in these docs. 这是这些文档中讨论的URL的唯一形式。 You no longer need to write @Component({ moduleId: module.id }), nor should you. 您不再需要编写@Component({moduleId:module.id}),也不需要。

Source : https://angular.io/docs/ts/latest/guide/change-log.html 来源: https : //angular.io/docs/ts/latest/guide/change-log.html

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

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