Angular 2中_co.routeToBrainstoming不是函数错误排查求助
_co.routeToBrainstoming is not a function Error in Angular 2 Hey there, let's walk through the most likely reasons you're hitting this error, and how to fix them. I've dealt with plenty of these "function not found" bugs in Angular, so here's what to check first:
1. Critical Typo in the Function Name (The #1 Culprit)
Look closely at the error message: it's looking for routeToBrainstoming (note the missing 'r' in "Brainstoming"). But your component defines routeToBrainstorming()—that's a clear mismatch!
9 times out of 10, this error happens because your template has a typo in the function call. For example, if you wrote:
<button (click)="routeToBrainstoming()">Go to Brainstorming</button>
You need to correct the spelling to match your component's method name:
<button (click)="routeToBrainstorming()">Go to Brainstorming</button>
Double-check every instance of the function call in your templates—typos like this are easy to miss but instantly break the template-component binding.
2. Context Binding Issues (Less Likely, But Worth Checking)
If your function is being called from an async context (like a nested promise callback) or a template directive that changes the component context (e.g., *ngFor, *ngIf), it might lose its reference to the component instance.
Since you said other functions follow the same structure and work, this is probably not the issue, but you can rule it out by either:
- Binding the function in your component's constructor:
constructor(private _nmapi: NmapiService, private router: Router) { this.routeToBrainstorming = this.routeToBrainstorming.bind(this); } - Or using an arrow function for the method (though standard method syntax works for template event bindings in most cases):
routeToBrainstorming = (): void => { // Your existing logic here }
3. Template Context Mismatch
Make sure the element calling routeToBrainstorming() is actually part of your component's template, not a child component or ng-template that has its own isolated context. For example, if you're using a child component and forgot to pass the method as an input, the _co context in the error would refer to the child component (which doesn't have the method), not your parent component.
4. Bonus: Fix the Unbound name Variable in Your Code
While this isn't causing the "function not found" error, I noticed your code uses name without a this. prefix:
.getService("user/add/" + name)
Unless name is a local variable defined inside the function, this will throw a "name is not defined" error once you fix the function binding issue. Update it to reference the component's property:
.getService("user/add/" + this.name)
内容的提问来源于stack exchange,提问作者Web Develop Wolf

