英文:
TypeError: _co.function is not a function
问题
我正在使用Angular,并尝试在本地主机上创建一个网页,但我无法使用函数。我认为这是一个路径问题,但我已正确声明了所有的路径。
花了4个小时尝试在Google上找到答案,但没有找到任何解决这个问题的东西。
我自己创建了2个组件,不是在创建Angular项目时默认创建的:
post-create.component.html
post-create.component.ts
从app文件夹中,它们都位于:
'./posts/posts-create/post-create.component.html'
'./posts/posts-create/post-create.component.ts'
我已经复制粘贴了文件夹的名称,以确保没有拼写错误。
post-create.component.ts看起来像这样:
import { Component } from '@angular/core';
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html'
})
export class PostCreateComponent {
newPost = '';
onAddPost() {
this.newPost = 'The users posts';
}
}
post-create.component.html只是填充了一些随机内容以尝试使其工作,但看起来像这样:
<h2>sup </h2>
我的app.component.html是:
<h1>Hello world </h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()">Save post </button>
<p>{{ newPost }}</p>
当单击按钮时,应该调用函数"onAddPost()",但没有。它会报错:
ERROR TypeError: _co.onAddPost is not a function
我不知道为什么会这样。我的app.module.ts文件如下:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PostCreateComponent } from './posts/posts-create/post-create.component';
@NgModule({
declarations: [
AppComponent,
PostCreateComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
所以你可以看到,我已经导入了
import { PostCreateComponent } from './posts/posts-create/post-create.component';
并将其添加到了声明中。
这段代码是我正在观看的视频的一部分,我只是跟着编写代码以入门。如果你想知道的话,它来自于这个视频:
非常感谢你提前的回答,抱歉浪费你的时间,我已经尝试自己修复这个问题,但似乎找不到为什么它不让我使用这个函数。
英文:
I am working with angular and trying to make a webpage on my local host, but I cannot use functions. I assume it is a path issue, but I have declared all my paths correctly.
Spent 4 hours trying to google the answer up, didn't find anything answering this issue.
I have created 2 components myself, outside of what was made by defualt when I created the angular project:
post-create.component.html
post-create.component.ts
From the app folder, they are both located at:
'./posts/posts-create/post-create.component.html'
'./posts/posts-create/post-create.component.ts'
I have been an copy pasted the name of the folders to make sure it isn't a miss-spell
post-create.component.ts looks like this:
import { Component } from '@angular/core';
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html'
})
export class PostCreateComponent{
newPost = '';
onAddPost(){
this.newPost = 'The users posts';
}
}
post-create.component.html is just filled with something random to try to make it work, but looks like this:
<h2> sup </h2>
My app.component.html is:
<h1> Hello world </h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()"> Save post </button>
<p>{{ newPost }}</p>
This should, when clicking the button call the function "onAddPost()" but does not. It gives this error:
ERROR TypeError: _co.onAddPost is not a function
and I have no clue why. My app.module.ts file is like this:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PostCreateComponent } from './posts/posts-create/post-create.component'
@NgModule({
declarations: [
AppComponent,
PostCreateComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
So as you can see, I have imported
import { PostCreateComponent } from './posts/posts-create/post-create.component'
And added it to the decleraction
The code is a part of a video that I am watching and just coding along with to get into it. If you wounder it is from this video:
https://www.youtube.com/watch?v=1tRLveSyNz8&fbclid=IwAR0k3FKxqbYVKuT3g2UgJVQWFW2xFXV8xmhzGbwqbyZ3ltWrBcVktYc38_I
Thank you for your answers in advance, sorry for taking up your time, I have tried to fix this myself but just can't seem to find out why it won't let me use the function
答案1
得分: 0
请查看1小时2分钟处的视频:
以下内容与post-create.component.ts相关(而不是app.component.ts):
<h1>你好,世界</h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()">保存帖子</button>
<p>{{ newPost }}</p>
英文:
Watch again the video at 1:02:00 :
This following content is related to post-create.component.ts (not to app.component.ts) :
<h1> Hello world </h1>
<textarea rows="6"></textarea>
<hr>
<button (click)="onAddPost()"> Save post </button>
<p>{{ newPost }}</p>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论