TypeError: _co.function is not a function

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

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看起来像这样:

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-post-create',
  4. templateUrl: './post-create.component.html'
  5. })
  6. export class PostCreateComponent {
  7. newPost = '';
  8. onAddPost() {
  9. this.newPost = 'The users posts';
  10. }
  11. }

post-create.component.html只是填充了一些随机内容以尝试使其工作,但看起来像这样:

  1. <h2>sup </h2>

我的app.component.html是:

  1. <h1>Hello world </h1>
  2. <textarea rows="6"></textarea>
  3. <hr>
  4. <button (click)="onAddPost()">Save post </button>
  5. <p>{{ newPost }}</p>

当单击按钮时,应该调用函数"onAddPost()",但没有。它会报错:

ERROR TypeError: _co.onAddPost is not a function

我不知道为什么会这样。我的app.module.ts文件如下:

  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { AppRoutingModule } from './app-routing.module';
  4. import { AppComponent } from './app.component';
  5. import { PostCreateComponent } from './posts/posts-create/post-create.component';
  6. @NgModule({
  7. declarations: [
  8. AppComponent,
  9. PostCreateComponent
  10. ],
  11. imports: [
  12. BrowserModule,
  13. AppRoutingModule
  14. ],
  15. providers: [],
  16. bootstrap: [AppComponent]
  17. })
  18. export class AppModule { }

所以你可以看到,我已经导入了

  1. import { PostCreateComponent } from './posts/posts-create/post-create.component';

并将其添加到了声明中。

这段代码是我正在观看的视频的一部分,我只是跟着编写代码以入门。如果你想知道的话,它来自于这个视频:

https://www.youtube.com/watch?v=1tRLveSyNz8&amp;fbclid=IwAR0k3FKxqbYVKuT3g2UgJVQWFW2xFXV8xmhzGbwqbyZ3ltWrBcVktYc38_I

非常感谢你提前的回答,抱歉浪费你的时间,我已经尝试自己修复这个问题,但似乎找不到为什么它不让我使用这个函数。

英文:

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:

  1. &#39;./posts/posts-create/post-create.component.html&#39;
  2. &#39;./posts/posts-create/post-create.component.ts&#39;

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:

  1. import { Component } from &#39;@angular/core&#39;;
  2. @Component({
  3. selector: &#39;app-post-create&#39;,
  4. templateUrl: &#39;./post-create.component.html&#39;
  5. })
  6. export class PostCreateComponent{
  7. newPost = &#39;&#39;;
  8. onAddPost(){
  9. this.newPost = &#39;The users posts&#39;;
  10. }
  11. }

post-create.component.html is just filled with something random to try to make it work, but looks like this:

  1. &lt;h2&gt; sup &lt;/h2&gt;

My app.component.html is:

  1. &lt;h1&gt; Hello world &lt;/h1&gt;
  2. &lt;textarea rows=&quot;6&quot;&gt;&lt;/textarea&gt;
  3. &lt;hr&gt;
  4. &lt;button (click)=&quot;onAddPost()&quot;&gt; Save post &lt;/button&gt;
  5. &lt;p&gt;{{ newPost }}&lt;/p&gt;

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:

  1. import { BrowserModule } from &#39;@angular/platform-browser&#39;;
  2. import { NgModule } from &#39;@angular/core&#39;;
  3. import { AppRoutingModule } from &#39;./app-routing.module&#39;;
  4. import { AppComponent } from &#39;./app.component&#39;;
  5. import { PostCreateComponent } from &#39;./posts/posts-create/post-create.component&#39;
  6. @NgModule({
  7. declarations: [
  8. AppComponent,
  9. PostCreateComponent
  10. ],
  11. imports: [
  12. BrowserModule,
  13. AppRoutingModule
  14. ],
  15. providers: [],
  16. bootstrap: [AppComponent]
  17. })
  18. export class AppModule { }

So as you can see, I have imported
import { PostCreateComponent } from &#39;./posts/posts-create/post-create.component&#39;
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&amp;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):

  1. <h1>你好,世界</h1>
  2. <textarea rows="6"></textarea>
  3. <hr>
  4. <button (click)="onAddPost()">保存帖子</button>
  5. <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) :

  1. &lt;h1&gt; Hello world &lt;/h1&gt;
  2. &lt;textarea rows=&quot;6&quot;&gt;&lt;/textarea&gt;
  3. &lt;hr&gt;
  4. &lt;button (click)=&quot;onAddPost()&quot;&gt; Save post &lt;/button&gt;
  5. &lt;p&gt;{{ newPost }}&lt;/p&gt;

huangapple
  • 本文由 发表于 2020年1月3日 22:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580507.html
匿名

发表评论

匿名网友

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

确定