Angular2组件为什么不渲染模板,而是渲染模板的路径?

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

Why Angular2 component don`t render template, but rendering path to template?

问题

我的Angular版本是2,我的组件在没有错误的情况下工作,但现在它不渲染模板,之前它是可以工作的。

我的组件

import template from "./edit.html";
import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  template,
})

我没有改动代码,Angular2应该会渲染模板的路径。

如何修复这个问题?

我尝试重新编译所有组件,但没有效果。

我尝试添加templateUrl: "./edit.html",但得到了以下结果:

Angular2组件为什么不渲染模板,而是渲染模板的路径?

英文:

I have an Angular version 2 my components is working without errors but now him don`t render template, early it is working

My component

import template from "./edit.html";
import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  template,
})

I don't changed code, Angular2 render path to template
Angular2组件为什么不渲染模板,而是渲染模板的路径?

How i can fix this ?

I try recompiling all components, its`not work

I try add `templateUrl: "./edit.html" and i get result Angular2组件为什么不渲染模板,而是渲染模板的路径?

答案1

得分: 2

直接在你的组件中直接设置templateUrl。无需导入它:

import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  templateUrl: "./edit.html",
})
英文:

Just set the templateUrl directly in your component. There's no need to import it:

import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  templateUrl: "./edit.html",
})

答案2

得分: 0

Angular默认不会读取所有的HTML内容,你必须在templateUrl中指定HTML文件的路径。在你的情况下,代码应该是这样的:

import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  templateUrl: "./edit.html"
})

现在Angular知道你的HTML代码在哪里了,希望这样可以正常工作。

英文:

Angular doesn't read all html by default, you have to mention html path inside templateUrl. In your case the code should be..

import { Subject } from "rxjs";

@Component({
  selector: "adminOffersEdit",
  templateUrl: "./edit.html"
})

Now with that angular knows where your html code is, hopefully this should work.

huangapple
  • 本文由 发表于 2023年2月8日 16:12:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382895.html
匿名

发表评论

匿名网友

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

确定