Skip to content

Instantly share code, notes, and snippets.

@bil0u
Last active September 24, 2020 09:57
Show Gist options
  • Save bil0u/b0d6edf7f9f499827aa34c9fbc5ec8e4 to your computer and use it in GitHub Desktop.
Save bil0u/b0d6edf7f9f499827aa34c9fbc5ec8e4 to your computer and use it in GitHub Desktop.
[NativeScript-Vue Class Components] Cheatsheet for Nativescript-Vue class component syntax #nativescript-vue #vuejs #cheatsheet

Input properties

Consider the following component :

<template>

	<Label :text="msg"/>

</template>

<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";

@Component
export default class MyComp extends Vue {

  	@Prop() private msg: string;
  
}
</script>

It will be used as :

<template>

	<MyComp msg="Hello World !"/>

</template>

Data

<template>

    <Label text="First Name"/>
	<TextField v-model="firstName"/>

</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";

@Component
export default class PersonClassComponent extends Vue {

  	firstName = "John";
  
}
</script>

Watch

<template>

    <Label text="First Name"/>
    <TextField v-model="firstName"/>

</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";

@Component
export default class PersonClassComponent extends Vue {
  
  	firstName = "John";

  	@Watch("firstName")
  	onFirstNameChanged() {
    	console.log('First name changed !');
  	}

}
</script>

Computed

<template>

	<Label text="Full Name"/>
    <Label :text="fullName"/>

</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";

@Component
export default class PersonClassComponent extends Vue {

	firstName = "John";
	lastName = "Doe";

	get fullName() {
		return `${this.firstName} ${this.lastName}`;
  	}

}
</script>

Methods

<template>

    <Button text="SPEAK" @tap="speak"/>
    
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";

@Component
export default class PersonClassComponent extends Vue {

  	speak() {
    	alert("This is your captain speaking !");
  	}
  
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment