【问题标题】:Typescript error TS1005: '=' expected打字稿错误 TS1005: '=' 预期
【发布时间】:2016-10-12 12:54:36
【问题描述】:

我在编译我的应用程序时不断收到此错误。 TS v2.0.3

app/components/profile.component.ts(13,12): error TS1005: '=' expected.
app/components/profile.component.ts(14,13): error TS1005: '=' expected.
app/components/profile.component.ts(16,10): error TS1005: '=' expected.

这是我的代码:

import { Component } from '@angular/core';

import { apiService } from '../services/api.service';

import 'rxjs/add/operator/map';

@Component({
    moduleId: module.id,
    selector: 'profile',
    templateUrl: './profile.component.html'
})
export class ProfileComponent{
    product[];
    products[];
    productTitle:string;
    first[];

    constructor(private _apiService:apiService){
        this._apiService.getProduct().subscribe(product => {
            //console.log(product.result.products);
            this.product = product.result.products[0];
        })
    }
    searchProd() {
        this._apiService.updateTitle(this.productTitle);
        this._apiService.getProduct().subscribe(productTitle => {
            //console.log(productTitle.result.products);
            this.first = productTitle.result.products[0];
            this.products = productTitle.result.products;
        })
    }
}

我读过这可能是因为 TS 更新了语法,并确实将其更新到了最新的 2.1.0 版本,但仍然出现错误。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • product[]; 不是公认的 Typescript 语法
  • 你能帮忙看看在这种情况下正确的语法是什么吗?
  • 您需要指定数组的类型,例如:products: any[];
  • 这取决于你想在这里声明什么。本能说您应该将其转换为卡坦建议的内容。但我看到您将 this.productthis.first 分配给单个元素,这使得声明 product: any[] 无效。
  • 另请注意,这对 2.0 来说并不新鲜——这从来都不是有效的语法

标签: angular typescript


【解决方案1】:

看起来您需要为这些行添加类型说明符,例如:

product:any[];
products:any[];
first:any[];

如果您有更明确的可用类型,请改用这些类型。

编辑

正如 cmets 关于问题的所述,使用 any 可能有其自身的问题。在这种情况下,您需要找到每个变量的正确类型并将any 替换为该类型。一种可能性是product,但如果没有看到更多代码,就很难分辨。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2018-03-06
    • 2013-08-20
    • 1970-01-01
    • 2016-01-11
    • 2019-05-09
    • 2018-05-14
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多