繁体   English   中英

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

[英]Property 'location' does not exist on type 'typeof window'

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

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

这是下面的代码

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");
        }

    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM