我可以将表示布尔值的字符串(例如,'true','false')转换为JavaScript中的内部类型吗?
我有一个隐藏的HTML格式,根据用户在列表中的选择进行更新.此表单包含一些表示布尔值的字段,并使用内部布尔值进行动态填充.但是,一旦将此值放入隐藏的输入字段,它就会变成一个字符串.
我可以找到确定字段的布尔值的唯一方法,一旦将其转换为字符串,就要依赖于字符串表示的字面值.
var myValue = document.myForm.IS_TRUE.value;
var isTrueSet = myValue == 'true';
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一目标?
我有一个computed方法:
computed: {
currentPosition () {
if(this.get_local_storage_state()){
return this.lastLocation
}
if (this.currentRestaurant) {
return this.currentRestaurant.address.location
} else if (this.searchPosition.lat && this.searchPosition.lon) {
return this.searchPosition;
} else {
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪个在我的电话中被调用<template>:
<GMap class="c-splitview__map" :markers="getMarkers" :position="currentPosition" :zoom="currentZoom" v-if="currentPosition" />
<div class="m-storefinder__placeholder" v-else>
<h1 class="o-headline">{{$tc("storefinder.emptyDeeplink")}}</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
并且出于某种原因,当它第一次被调用时,它的运行方式应该如何,但是当我再次尝试调用它时(重新渲染Vue组件)它不会被调用.
但!
当我注释掉第一个if()语句时,如下:
computed: {
currentPosition () {
// if(this.get_local_storage_state()){
// return this.lastLocation
// }
if (this.currentRestaurant) {
return this.currentRestaurant.address.location
} else if (this.searchPosition.lat && this.searchPosition.lon) {
return …Run Code Online (Sandbox Code Playgroud)