TimePicker - retrieving actual values selected (hour and minute) - vue.js

will post my question here since the vue.js forum seems dead and can't get slack confirmation email.
I recently started coding with .vue and have a question.
After I select a time using timepicker, I would like to have the hour and minute into a variable. (one or multiple, not sure yet)
Right now I get this "Sun Dec 31 1899 07:25:00 GMT-0500 (EST)"
I thought I could do
console.log(this.hour);
console.log(this.minute);
console.log(this.hour + ":" + this.minute);
the code his here
https://play.nativescript.org/?template=play-vue&id=pFJlwX&v=4
<template>
<Page>
<ActionBar title="Time Picker" />
<ScrollView>
<StackLayout class="home-panel">
<TimePicker v-model="yourTimeValue" #timeChange="onHour" :hour="8" :minute="currentMinute" minuteInterval="5" />
<TextField v-model="textFieldValue" hint="Enter text..." #returnPress=" onHour" />
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
export default {
data() {
return {
currentMinute: new Date().getMinutes(),
textFieldValue: ""
};
},
methods: {
// when you release your finger from the timepicker, you end up in this function
onHour: function(args) {
console.log(this.yourTimeValue); // returns something like this Sun Dec 31 1899 07:25:00 GMT-0500 (EST)
console.log(this.textFieldValue); // this return the text you wrote in the text field
}
}
};
</script>
<style scoped>
.home-panel {
vertical-align: center;
font-size: 20;
margin: 15;
}
.description-label {
margin-bottom: 15;
}
</style>
Thank you

v-model can hold only one value so it's Date which includes the Time (hour & minutes).
You can always parse the hour and minutes from the date object,
console.log(this.yourTimeValue.getHours() + ":" + this.yourTimeValue.getMinutes());

Related

NativeScript Vue.js add a button linking to a website

I'm using Nativescript playground IOS and I'm new to Nativescript. I'm trying to add a button linking to a website when clicked.
<template>
<Page>
<ActionBar title="About Me" />
<ScrollView>
<StackLayout class="home-panel">
<!--Add your page content here-->
<Image
src="https://nexus.leagueoflegends.com/wp-content/uploads/2020/01/01_Banner_Fiddlesticks_Concept_9j7dhjnqxbpc7o0qibqp.jpg"
style="height:800px" />
<Label textWrap="true" text="Play with NativeScript!"
class="h2 description-label" />
<Label textWrap="true"
text="Favorite Type Of Noodles: Ravioli."
class="h2 description-label" />
<Button text="Button" #tap="onButtonTap" class="-primary" />
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
export default {
methods: {
onItemTap: function(args) {
console.log("Item with index: " + args.index + " tapped");
},
onButtonTap() {
console.log("Button was pressed");
}
},
data() {
return {};
}
};
</script>
<style scoped>
.home-panel {
vertical-align: center;
font-size: 20;
margin: 15;
}
.description-label {
margin-bottom: 15;
}
</style>
I tried location.href = 'www.yoursite.com';
But the app just crashes for some reason.
Any help would be appreciated!
You should be able to use openUrl to open a website on your browser:
import { openUrl } from "tns-core-modules/utils/utils";
openUrl('your website url here');

v-if to toggle message is resulting in infinite update loop Vue warning

I am learning Vue Native, specifically the v-if conditional, and have the following test code:
<template>
<view class="container">
<button :on-press="seen = !seen" title="Click to Toggle Message Visibility" />
<text v-if="seen">Now you see the message</text>
</view>
</template>
<script>
export default {
data: function() {
return {
seen: false
};
}
};
</script>
<style>
.container {
flex: 1;
align-items: center;
justify-content: center;
}
</style>
It is supposed to let the user click the button and the message is to appear/disappear. However, it is resulting in the following error:
console.error: "[Vue warn]: You may have an infinite update loop in a component render function. (found in )"
How should the code be modified so it works?
The problem is that vue is treating :on-press="seen = !seen" like a binding so it always executes the expression on render. The rendering is modifying the state, hence causing an infinite loop.
I think you just need to extract it in a function.
<template>
<view class="container">
<button :on-press="onPress" title="Click to Toggle Message Visibility" />
<text v-if="seen">Now you see the message</text>
</view>
</template>
<script>
export default {
data: function() {
return {
seen: false
};
},
methods: {
onPress() {
this.seen = !this.seen;
}
}
};
</script>
<style>
.container {
flex: 1;
align-items: center;
justify-content: center;
}
</style>
Agree with the accepted answer. In addition, the :on-press is an attribute binding, but using v-on event binding will not have this infinite loop issue, for example, like <button v-on:click="seen = !seen" title="Click to Toggle Message Visibility" />. That's probably where you get this inline method usage seen = !seen from.

Confused about printing "object.value" in nativescript-vue

Below nativescript-vue code I'm parsing a json file and trying to print it, but some reason I can't print json.city.value in Label, but I can print it inside script part, inside readFromJson method console.log(json.city.value) is working, and in Label tag also I can print parsedJson.city, but not parsedJson.city.value is value a keyword or preserved variable for nativescript or vuejs?
Below you can find, simplified code of what I'm trying to do.
<template>
<Page>
<ActionBar title="Welcome to NativeScript-Vue!"/>
<GridLayout columns="*" rows="*">
<Label class="message" :text="parsedJson.city.value" col="0" row="0"/>
</GridLayout>
</Page>
</template>
<script >
let json2 = '{"approved":{"type":"Boolean","value":true,"valueInfo":{}},"city":{"type":"String","value":"Konstanz","valueInfo":{}},"street":{"type":"String","value":"Reichenaustraße 11","valueInfo":{}},"contractNumber":{"type":"String","value":"3847384contractNumber","valueInfo":{}},"description":{"type":"String","value":"","valueInfo":{}}}'
export default {
data() {
return {
msg: 'Hello World!',
parsedJson: {}
}
},
mounted() {
console.log("mounted is worked")
console.log("json to parse is: " + json2)
this.readFromJson(json2)
},
methods: {
readFromJson(input) {
console.log("inside readfromjson")
var json = JSON.parse(input)
console.log("parsed city.value is: " + json.city.value)
this.parsedJson = json
}
}
}
</script>
<style scoped>
ActionBar {
background-color: #53ba82;
color: #ffffff;
}
.message {
vertical-align: center;
text-align: center;
font-size: 20;
color: #333333;
}
</style>
If I had to guess is that it immediately tries to use a value that is undefined. parsedJson is defined as {} and doesn't get initialized till later. Maybe try initializing it an empty string:
parsedJson: {
city: {
value:''
}
}

Vue JS code is not updating inside a loop

I'm stuck with a problem for a couple of days now and I've no idea what might be the solution. I've tried a couple, nothing worked.
I'm trying to enlarge a span text to fit the parent container width.
I've tried this.$forceUpdate();, didn't work.
I've also tried to pause the loop, but I found out later that that's not really possible in JS.
<template>
<span
ref="textRowRef"
v-bind:style="{fontSize: fontSize + 'px'}" >
{{ textRow }}</span>
</template>
// this is the VueJS code
var textRow = this.$refs.textRowRef;
var parentRow = textRow.parentElement;
for(var i = 0; i < 10; i++) {
this.fontSize += 10;
console.log(`Text Row in loop: ${textRow.clientWidth}`);
textRow = this.$refs.textRowRef;
}
console.log(`Text Row: ${textRow.clientWidth}`);
console.log(`Parent Row: ${parentRow.clientWidth}`);
Results in the console:
10 Text Row in loop: 48
Text Row: 48
Parent Row: 378
Here's my attempt.
I set fontSize to 72px then gradually reduce it by 1px until the text fits within the box. For such a simple example the performance is fine but I suspect that in the context of a larger page reducing it by 1px each time might prove too slow.
Each time the fontSize changes it triggers a re-render. The updated hook then measures the text to decide whether it needs to be shrunk even further.
If the text changes it resets the size to 72px and starts the process over again. I've not made any attempt to track the size of the parent element but it would also need to perform a reset if the width of that element changed.
I've added whitespace: nowrap to the <span> to ensure that its width will overflow the parent element. Otherwise the text would just wrap when the width reaches the edge of the parent element.
const MAX_FONT_SIZE = 72
new Vue({
el: '#app',
data () {
return {
fontSize: MAX_FONT_SIZE,
textRow: 'Initial value for the text'
}
},
watch: {
textRow: 'resetSize'
},
mounted () {
this.refreshSize()
},
updated () {
this.refreshSize()
},
methods: {
async refreshSize () {
await this.$nextTick()
const textRow = this.$refs.textRowRef
const parentRow = textRow.parentElement
if (textRow.offsetWidth > parentRow.clientWidth) {
this.fontSize = Math.max(8, this.fontSize - 1)
}
},
resetSize () {
if (this.fontSize === MAX_FONT_SIZE) {
this.refreshSize()
} else {
this.fontSize = MAX_FONT_SIZE
}
}
}
})
#app {
background: #ccc;
border: 1px solid #000;
margin: 20px;
width: 300px;
}
.text-span {
white-space: nowrap;
}
<script src="https://unpkg.com/vue#2.6.10/dist/vue.js"></script>
<div id="app">
<span
ref="textRowRef"
class="text-span"
:style="{fontSize: fontSize + 'px'}"
>
{{ textRow }}
</span>
<br>
Edit: <input v-model="textRow">
</div>

Nativescript-vue display an image on tap event

very new to Nativescript and Vue so please excuse my half baked question.
I am trying to build a small app in which i have an image tag and a button, on press of a button, the image should display, i would like this image source to be passed on as a variable, but on pressing the button, my app crashes, i am writing this in nativescript playground.
here is the code:
<template>
<Page class="page">
<ActionBar title="Home" class="action-bar" />
<ScrollView>
<StackLayout class="home-panel">
<Image src="{{img_src}}" #tap="onImgTap" />
<Button text="Button" #tap="onButtonTap" />
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
const imageSourceModule = require("tns-core-modules/image-source");
const imageSourceModule = require("tns-core-modules/image-source");
const fileSystemModule = require("tns-core-modules/file-system");
export default {
methods: {
onButtonTap() {
console.log("Button was pressed");
img_src:native_img
},
onImgTap(){
alert("Dont press image !");
}
},
data() {
return {
native_img:"https://play.nativescript.org/dist/assets/img/NativeScript_logo.png"
};
},
}
</script>
<style scoped>
.home-panel {
vertical-align: center;
font-size: 20;
margin: 15;
}
.description-label {
margin-bottom: 15;
}
</style>
really appreciate any help.!
Regards,
~Harry
Your app is crashing as your to assign value to variable img_src:native_img is incorrect.
It should be,
onButtonTap() {
console.log("Button was pressed");
this.img_src=native_img;
}
Also in data need to define img_src:""
data() {
return {
native_img:"https://play.nativescript.org/dist/assets/img/NativeScript_logo.png",
img_src:""
};
}
Also in Html, src={{img_src}} to :src=img_src
<Image :src="img_src" #tap="onImgTap" />