【发布时间】:2019-08-16 16:35:26
【问题描述】:
我想知道是否有人遇到过类似我现在遇到的问题?
我正在学习 Scrimba 的角度课程,它的第 7 课,角度组件。
我使用 cli 命令创建了 angular 应用程序并尝试加载和更改“正在加载...” 像讲座一样带有“hello world”的部分, 甚至复制了整个代码并将其粘贴到我的本地文件中并刷新 index.html 但仍然不起作用!
不知道有没有人知道或遇到过类似的问题? 非常感谢您的帮助!
这就是我的 app.component
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
template: `<h1>{{ title }}</h1>`,
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass']
})
export class AppComponent implements OnInit {
title: string;
constructor(){ }
ngOnInit() {
this.title = 'hello';
}
}
这就是我的 app.module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CustomersComponent } from './customers/customers.component';
@NgModule({
declarations: [
AppComponent,
CustomersComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的 index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularProject</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root>
Loading...
</app-root>
</body>
</html>
【问题讨论】:
-
我们不知道你做了什么改变(我们不知道这个讲座是什么),我们不知道“不起作用”是什么意思。准确地告诉你你在做什么,你期望发生什么,以及会发生什么。贴出相关代码。
-
这并不意味着什么。你显然忘了说完你的句子。
-
@JBNizet 我试图了解角度组件的工作原理以及如何使用 ngOnInit 函数,而我期望发生的是在“app-root”中,默认文本是“正在加载...”但我想在触发 ngOnInit 函数时添加“hello”文本。但目前,它没有在浏览器/html 中添加“hello”。希望这现在清楚了吗?你能帮我吗?
-
默认文本未加载。加载是在浏览器加载页面到浏览器解析和执行 JavaScript 代码之间出现的情况。此时,Angular 会实例化组件,并用组件的动态模板替换默认的静态文本“Loading”。正如您已经被告知的那样,您不能同时拥有模板和模板Url。你想保留哪一个?如果是 templateUrl,app.component.html 文件包含什么?您希望看到什么,而您又看到了什么?
标签: javascript angular typescript single-page-application angular8