Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

vue 中如何判断undefined类型?

created() {
        //如果取出来的可能是 undefined 类型 怎么判断?
        this.username = localStorage.getItem("username");
        
        //网上找的方式,发现没什么用啊!!
        if((typeof this.username) == 'undefined'){
            this.username="aaa";
        }
        //三等号也不行
        if((typeof this.username) === 'undefined'){
            this.username="aaa";
        }
        //不加引号 也没用
        if((typeof this.username) === undefined){
            this.username="aaa";
        }
      
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

首先,storage 里面就不支持别的类型,只支持字符串,所以你判断不出来存入的是字符串 'undefined' 还是 undefined,也判断不出来数值 1 还是字符串 '1'

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...