TypeError: _co.function is not a function

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

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';

并将其添加到了声明中。

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

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:

&#39;./posts/posts-create/post-create.component.html&#39;
&#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:

import { Component } from &#39;@angular/core&#39;;


@Component({
  selector: &#39;app-post-create&#39;,
  templateUrl: &#39;./post-create.component.html&#39;
})
export class PostCreateComponent{
  newPost = &#39;&#39;;

  onAddPost(){
    this.newPost = &#39;The users posts&#39;;
  }
}

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

&lt;h2&gt; sup &lt;/h2&gt;

My app.component.html is:

&lt;h1&gt; Hello world &lt;/h1&gt;
&lt;textarea rows=&quot;6&quot;&gt;&lt;/textarea&gt;
&lt;hr&gt;
&lt;button (click)=&quot;onAddPost()&quot;&gt; Save post &lt;/button&gt;
&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:

import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { NgModule } from &#39;@angular/core&#39;;

import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { PostCreateComponent } from &#39;./posts/posts-create/post-create.component&#39;

@NgModule({
  declarations: [
    AppComponent,
    PostCreateComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
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):

<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) :

&lt;h1&gt; Hello world &lt;/h1&gt;
&lt;textarea rows=&quot;6&quot;&gt;&lt;/textarea&gt;
&lt;hr&gt;
&lt;button (click)=&quot;onAddPost()&quot;&gt; Save post &lt;/button&gt;
&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:

确定