【问题标题】:Vuex Typescript I am getting the error "Member 'someMutation' implicitly has an 'any' type." in the Component.ts fileVuex Typescript 我收到错误“成员 'someMutation' 隐含地具有 'any' 类型。”在 Component.ts 文件中
【发布时间】:2019-03-16 21:06:16
【问题描述】:

我收到错误:

“成员 'someMutation' 隐含地具有 'any' 类型。”。

此错误出现在我的 Vue 组件中 Component.ts 文件中的突变,从 Vuex 商店收到。

我在Component.ts中有以下代码:

@WithRender
@Component({
    components: { SomeChildComponent },
})
export default class Component extends Vue {
    constructor() {
        super();
        this.someFunc(this.someComponentMethod);
        // ... some logic
    }
    @Mutation public someMutation;

    private someComponentMethod(str: string) {
        this.someMutation();
    }
}

用于突变的代码:

const mutations: MutationTree<State> = {
    someMutation(state, name: string): void {
        state.prop = state.prop.filter((el: IEl): boolean => el.name !== name);
    },
};

const c = {
    state,
    getters,
    mutations,
};

const store = new Vuex.Store(c);

我尝试了一些变体,例如@Mutation public someMutation: void; 或@Mutation public someMutation(s: string): void;,但没有成功。

我的目的是解决问题,而不仅仅是关闭 tslint.json 文件中的规则 NoImplicitAny。

【问题讨论】:

    标签: typescript vue.js vuejs2 vuex


    【解决方案1】:

    根据someMutation的来源,该方法接受一个名为name的string参数,并返回void,其类型如下:

    (name: string) => void
    

    由于属性是由vuex-class自动初始化的,所以我们可以使用definite assignment assertion来避免“no initializer”错误。

    !: (name: string) => void
    ^
    

    最终结果是:

    @Mutation someMutation!: (name: string) => void
    

    【讨论】:

    • 托尼,非常感谢您的回答!这个解决方案绝对解决了我的问题!)你是打字稿大师!再次感谢!:)
    • @Vika 没问题 :)
    猜你喜欢
    • 2018-08-23
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2022-10-08
    • 2019-08-04
    相关资源
    最近更新 更多