类型“ typeof window”上不存在属性“ location”

时间:2018-11-12 11:50:19

标签: javascript reactjs typescript

我的代码一直工作到升级打字稿和对框架进行响应。此错误发生在 window.location.assign(“#/ home / dashboard”); 行上。我已阅读文章,但仍无法解决问题。我还认为window属性来自 modernizer.js。  当前,尝试运行项目时出现的错误如下:

  

TS2339:“类型窗口”类型不存在属性“位置”。

这是下面的代码

import RaisedButton from "material-ui/RaisedButton";
import {ActionLockOutline, SocialPerson} from "material-ui/svg-icons";
import * as React from "react";
import {hot} from "react-hot-loader";
import { OAuth } from "../../Shared/Services/OAuth";
import { Component, IComponentState } from "../../Shared/Utilities/Component";
import { Log } from "../../Shared/Utilities/LogUtil";
import { Validation } from "../../Shared/Utilities/Validation";

// import { StorageUtil } from "../../Shared/Utilities/StorageUtil";

interface ILoginState extends IComponentState {
    userName: string;
    password: string;
}

class Form extends Component<any, ILoginState> {

    constructor(props: any) {
        super(props,
            {
                password: "",
                userName: "",
            });

    }

    public loginClick = () => {
        // this.warningNoti("All fields marked red are required");

        if (Validation.formValidation("#loginForm")) {
            OAuth.userLogin(this.state.userName, this.state.password, (loginResponse: any) => {
                Log.consoleObj(loginResponse);

                this.successNoti("User successfully logged in");

                window.location.assign("#/home/dashboard");

            }, (status: string, jqXhr: any) => {
                // console.log(status);
                // console.log(jqXhr);
                this.setState({password: ""});
                this.infoNoti("Incorrect username or password, please check credentials.");
            });
        } else {
            this.warningNoti("All fields marked red are required");
        }

    }
}

1 个答案:

答案 0 :(得分:0)

首先回答您的问题:为什么tslint会提出“窗口”类型上不存在“属性”位置。这是因为Typescript定义了一些类型集,默认类型和自定义类型。对于Typescript窗口是自定义类型。默认类型为数字,字符串,布尔值,任意值等。因此,当您要使用窗口时,它被视为具有默认值lib.dom.d.ts中定义为接口的具有赋值,位置等功能的窗口对象。现在,我建议您创建一个const类型设置为any的变通办法。 参考:https://www.typescriptlang.org/docs/handbook/basic-types.html

解决方案/解决方法:通过执行以下操作,自行设置常量的类型:

const temp: any = window;
temp.location.assign('#/home/dashboard');
相关问题